Pointer Null
Pointer Null

Reputation: 40380

Listview + BaseAdapter - how to notify about change in single item?

I have a list view fed by BaseAdapter.

When something changes in data, I call BaseAdapter.notifyDataSetChanged. All works fine.

Now I wonder, if I change some tiny detail in some list item, is there some optimized way to update view of single item if it's displayed on screen? I suppose that notifyDataSetChanged blindly rebuilds views of all visible items in list, which is not optimal if trivial change in item happens.

Upvotes: 4

Views: 4517

Answers (2)

Pointer Null
Pointer Null

Reputation: 40380

Yes, there is way. Assuming I can identify list item, for example by assigning View.setTag to it to some value, I can iterate list items and rebind only one list item if desired, or even update only some sub-view of the item.

It's simple and relatively cheap (linear search):

for(int i = list.getChildCount(); --i>=0; ){
   View v = list.getChildAt(i);
   Object id = v.getTag();
   if(id==myId){
      updateListItem(id, v);
      break;
   }
}

where myId is some ID of item I want to update, and updateListItem makes desired changes on the list item. Proved, and working fine and very efficiently.

Upvotes: 1

Philipp F
Philipp F

Reputation: 924

No, I guess it's not possible unless you implement your own ListView class extension. Here there is the source code for setAdapter().

You will see, the ListView only registers itself as observer using mAdapter.registerDataSetObserver(mDataSetObserver); And a DataSetObserver provides no means to notify about a change at a certain position.

However it might not be necessary to notify about updates of certain items, because as far as I know, a ListView only renders and updates the items currently seen on the screen so, no optimization should be necessary here.

Upvotes: 1

Related Questions