2

I have an ArrayAdapter holding a list of updating elements (e.g., a list of async-loaded news items).

What is the proper pattern so that, when an element's value changes, it updates the View?

Tordek
  • 10,628
  • 3
  • 36
  • 67

2 Answers2

1

Use notifyDataSetChanged to reflect updates in your listView

adapter.notifyDataSetChanged();
Robin Chander
  • 7,225
  • 3
  • 28
  • 42
  • Does this trigger an update on the whole view? Isn't this ineficient? (Say I only update a single element.) – Tordek Dec 09 '12 at 01:25
  • updating just one element is much less convenient. but it can be done by referring to a specific view by calling `getChildAt` at a position on your listview. the problem is that this will only be valid for visible row positions on screen so the workaround is to subtract this position by the first visible position on the screen http://stackoverflow.com/questions/3724874/how-can-i-update-a-single-row-in-a-listview – mango Dec 09 '12 at 09:22
1

You can use methods like ArrayAdapter#add(), ArrayAdapter#addAll(), etc. These will update the View that is bound to the adapter automatically.

Or you can use the same methods on the List that the adapter reads and call ArrayAdapter#notifyDataSetChanged() yourself.

Sam
  • 86,580
  • 20
  • 181
  • 179
  • add and addAll aren't what I'm looking for: I'm referring to updating a single element on the Adapter. – Tordek Dec 09 '12 at 01:26
  • As you probably noticed there is no `update()` or `replace()` method, your question lacks specifics so I still don't know what you are looking for... You can try `insert()` and `remove()` or maybe `add()`, `remove()`, and `sort()`, but again I have no idea what type of data working with. – Sam Dec 09 '12 at 17:22