Duplicated Entries in Listview

Duplicated entries in ListView

Try this:

public View getView(int position, View convertView, ViewGroup parent) {

if (null == convertView) {
LinearLayout view = (LinearLayout) LinearLayout.inflate(this.context,
R.layout.message, null);
Log.d("SeenDroid", String.format("Get view %d", position));
TextView title = new TextView(view.getContext());
title.setText(this.items.get(position).getTitle());
view.addView(title);
return view;
} else {
LinearLayout view = (LinearLayout) convertView;
TextView title = (TextView) view.getChildAt(0);
title.setText(this.items.get(position).getTitle());
return convertView;
}
}

Explanation: you got the duplicates because lists on Android reuse UI objects. You are expected to reuse convertView rather than create a new one if it is not null. Of course, you are responsible to set an appropriate value to the instance being reused. Otherwise the value is left from the last "usage".

Duplicate values in listview in android

Try to clear list on every start, its help my when I use RecyclerView, I think here same problem

ListView duplicating after adding new items to Firebase

Every time there's a change to databasereference, your onDataChange gets called with a full snapshot of the data at that path. So even if only one child node was changed/added/removed, the snapshot also contains all other (unmodified) child nodes that you already added to registrattionNumber.

The simplest solution is to clear registrattionNumber at the top of onDataChange:

databasereference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
registrattionNumber.clear();
...

Also see:

  • Duplicate objects from Firebase loaded into ListView
  • Duplicate data from firebase realtime databse
  • Item gets duplicated in adapter, when we delete or update item from Firebase Realtime Database
  • Why do i get duplicate values when adding data to firebase realtime database using kotlin?
  • And many more from this search: https://stackoverflow.com/search?q=%5Bfirebase-realtime-database%5D%5Bandroid%5D+duplicate

Listview items duplicated when scrolling

You assign your "UI"-Variables only when there is no view to recycle (view == null).
Maybe should use the ViewHolder Pattern. For the moment, the following should be ok:

    View view;
if (null != convertView) {
view = convertView;
} else {
view = getActivity().getLayoutInflater().inflate(R.layout.item_fans_task, null);
}
taskImg = (ImageView) view.findViewById(R.id.task_img);
taskTitleText = (TextView) view.findViewById(R.id.task_title_text);
taskContentText = (TextView) view.findViewById(R.id.task_content_text);
taskProcessImg = (ImageView) view.findViewById(R.id.task_process_img);
shareText = (TextView) view.findViewById(R.id.share_text);
taskType = (TextView) view.findViewById(R.id.task_type);

Duplicates in listview - Android

I think the solution is actually in the first answer you linked to.

Try changing your code to something like:

if(view ==null)
{
view = inflater.inflate(R.layout.green,viewGroup,false);
vh = new ViewHolder(view);
view.setTag(vh);
vh.tv.setText(songs.get(i));
}
else{
vh = (ViewHolder) view.getTag();
vh.tv.setText(songs.get(i));
}

In the else statement, you're reusing a view that's already been created, however you still need to update the data in that view.



Related Topics



Leave a reply



Submit