How to call notifyDataSetChanged () from a generic adapter

An OnItemClickListener for a ListView has the following method:

 @Override public void onItemClick(AdapterView<?> parent, View v, int position, long id) 

I would like to have an adapter for updating ListView , and I believe that this is done with:

 BaseAdapter.notifyDataSetChanged() 

How to use parent parameter in onItemClick method for this? So far I have tried:

 parent.getAdapter().notifyDataSetChanged(); 

This causes an error because the returned object has the HeaderViewListAdapter class, which for unknown reasons is not a subclass of BaseAdapter .

+4
source share
5 answers

AFAIK, there is no method in the HeaderListView for updating / reloading data. The only way I can do this is to reassign the adapter.

+2
source

You can also go to the base adapter through your custom adapter, as shown in the following code snippet:

 public class HeaderViewListAdapter extends BaseAdapter { ... @Override public void notifyDataSetChanged() { super.notifyDataSetChanged(); } ... } 
+5
source

This causes an error because the returned object has the HeaderViewListAdapter class, which for unknown reasons is not a subclass of BaseAdapter.

 ((BaseAdapter) ((HeaderViewListAdapter)listView.getAdapter()).getWrappedAdapter()).notifyDataSetChanged(); 
+5
source

You do not need to call any method to update the adapter client when working with the cursor. Instead, after any operation, you should “Report Changes” using getContentResolver().notifyChange(uri, null);

For instance:

 context.getContentResolver().update(SomeURI.CONTENT_URI, values, null,null); context.getContentResolver().notifyChange(SomeURI.CONTENT_URI, null); 

Your viewclient (Listview, Gridview, Whatever) will automatically change even if you use a custom adapter or not. I hope for this help.

+2
source

I found a much simpler way ... just call this.onCreate(null) , which will reload the full create method, but the action will remain the same.

0
source

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


All Articles