Android - Arrayadapter Duplicates Listview Items
Solution 1:
It looks like you've already tracked down the answer. By providing the ArrayList
to the ArrayAdapter
, you have, effectively, coupled the two. In order to update the data, all you need to do is add a new element into the ArrayList
.
The .add()
method is just a shortcut to this. All it does is add the provided element into the ArrayList
. You can add the element either way, just don't do it both ways.
What's the point of notifyDataSetChanged()
then? Just because you update the ArrayList
doesn't mean that the ListView
knows there is new data. Thus, new data might exist, but the view might not refresh itself. Calling notifyDataSetChanged()
tells the ListView
to look for new data and redraw itself so that the user sees the change.
From the Android documentation for ArrayAdapter.notifyDataSetChanged()
:
Notifies the attached observers that the underlying data has been changed and any View reflecting the dataset should refresh itself.
http://developer.android.com/reference/android/widget/ArrayAdapter.html#notifyDataSetChanged()
Solution 2:
On your adapter, verify if the view is null, if it is, assign the proper views from your layout, after the if, set the values.
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
viewHolder = new ViewHolder();
LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.customswipelayout, parent, false);
viewHolder.name = (TextView) convertView.findViewById(R.id.rowTextView1);
viewHolder.price = (TextView) convertView.findViewById(R.id.rowTextView2);
viewHolder.stockCount = (TextView) convertView.findViewById(R.id.rowTextView3);
convertView.setTag(viewHolder);
mItemManger.bindView(view, position);
}
/* rest of your adapter code */
}
This is an example from one of mine, after I close the if statement, I assign my values and return the view:
viewHolder = (ViewHolder) view.getTag();
final Object object = this.list.get(position);
ImageView cmdDelete = (ImageView) view.findViewById(R.id.cmdDelete);
cmdDelete.setOnClickListener
//more personal code follows
return view;
Now I scroll through my list and it works perfectly!
Post a Comment for "Android - Arrayadapter Duplicates Listview Items"