How to call notifyDataSetChanged() from a generic Adapter

房东的猫 提交于 2019-12-07 09:35:22

问题


An OnItemClickListener for a ListView has the following method:

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

I'd like to have the adapter behind the ListView refresh, and I believe this is done through the use of:

BaseAdapter.notifyDataSetChanged()

How do I use the parent parameter in the onItemClick method to do this? So far I've tried:

parent.getAdapter().notifyDataSetChanged();

This throws an error because the object returned is of class HeaderViewListAdapter, which for reasons unknown isn't a subclass of BaseAdapter.


回答1:


AFAIK, there isn't any method in HeaderListView for data refresh/reload. The only way I can think of doing this is to reassign the adapter.




回答2:


You can also get to the BaseAdapter via your custom adapter as show in the following code fragment:

public class HeaderViewListAdapter extends BaseAdapter {

...

  @Override
  public void notifyDataSetChanged() {
    super.notifyDataSetChanged();
  }

...
}



回答3:


This throws an error because the object returned is of class HeaderViewListAdapter, which for reasons unknown isn't a subclass of BaseAdapter.

((BaseAdapter) ((HeaderViewListAdapter)listView.getAdapter()).getWrappedAdapter()).notifyDataSetChanged();



回答4:


You don't need to call any method to refresh your adapter client when you work with cursor. Instead, after any operation you have to "Notify the changes" using getContentResolver().notifyChange(uri, null);

For example:

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

Your viewclient (Listview, Gridview, Whatever) will change automatically even you're using a custom adapter or not. Hope this help.




回答5:


I found much easier way ..just call this.onCreate(null) that will reload complete create method but the activity remains same .



来源:https://stackoverflow.com/questions/2867469/how-to-call-notifydatasetchanged-from-a-generic-adapter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!