Duplicated entries in ListView Duplicated entries in ListView android android

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".


public View getView(int position, View convertView, ViewGroup parent) {    LayoutInflater inflater = (LayoutInflater) context        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);    View gridView;    if (convertView == null) {        gridView = new View(context);    } else {        gridView = (View) convertView;    }    gridView = inflater.inflate(R.layout.worker_listmain, null);    // your source code here!!! Run 100%    // I got this problem also, I found out the way to solve it!     // Please use my source code :D SIMPLE is PERFECT :D    return gridView;}


ListView does not guarantee uniqueness of items you are added there. It is your responsibility. You are using ArrayList to store items and it can store as many duplicate items as you want.

If you want to removed duplicates put your items into set and then into list again:

listMessages = new ArrayList<Messages>(new LinkedHashSet<Message>(listMessages))

The LinkedHashSet will remove duplicates preserving the initial order of items, ArrayList will allow access elements by position.