ListView does not update when calling notifyDataSetChanged from a BaseAdapter

前端 未结 3 1299
挽巷
挽巷 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: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.

提交回复
热议问题