How to set a listener in a ListView that starts when list data changes?

I have a ListView with a custom adapter. I want to connect a listener to this ListView, which runs whenever the data in the ListView changes, as this can happen in a wide variety of ways, and I need a different view to update whenever the ListView is updated. Essentially, I want this to work whenever a notifyDataSetChanged () is called directly or indirectly (via add (), remove (), etc.):

Whenever an item in the list is deleted, I want this to call. Whenever an item is added to the list, I want it to call. etc. etc.

Should I create my own listener for this (and if so, a short explanation of how this will be very useful) or is there a built-in way to listen to these events?

+5
source share
1 answer

I found a solution:

_adapter = new CustomListAdapter(this.getActivity(), _list); _adapter.registerDataSetObserver(new DataSetObserver() { @Override public void onChanged() { // update other view } }); 

It works exactly as I hoped!

+18
source

Source: https://habr.com/ru/post/1214522/


All Articles