ListView does not update when calling notifyDataSetChanged from a BaseAdapter

前端 未结 3 1288
挽巷
挽巷 2021-01-17 21:21

im am having difficulties to update a ListActivity when the underlying data changes.

I am using a custom (list) adapter (CustomListAdapter) derived vom BaseAdapter t

相关标签:
3条回答
  • 2021-01-17 22:13

    The issue is resolved. The problem was in fact at a different point (an intermediate class that was not mentioned here didn't react appropriately to changes). The initial code works beautifully.

    Thanks alot for the effort,

    0 讨论(0)
  • 2021-01-17 22:19

    The registerDataSetObserver() part of the Adapter interface is for any external objects who might be interested to know when the data set changes. A ListView shouldn't really be interested in these methods... if it's BaseAdapter content changes, you call BaseAdapter.notifyDataSetChanged() which will tell the ListView to update itself.

    In other words you only need to make the following tiny change:

    public void setValue(Object newValue) {
         this.value = newValue;
         notifyDataSetChanged();
    }
    

    Actually, since you're changing the state of an existing item (rather than adding new ones etc) then notifyDataSetInvalidated() would be a better choice.

    And of course you don't need any of that DataSetObserver stuff unless you actually do have other objects elsewhere that need to know about this data.

    0 讨论(0)
  • 2021-01-17 22:19
    adapter.notifyDataSetChanged();
    
    0 讨论(0)
提交回复
热议问题