Search code examples
androidandroid-listviewlistadapter

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.


Solution

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