Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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'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.

like image 960
Matty F Avatar asked Dec 09 '22 16:12

Matty F


2 Answers

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();
  }

...
}
like image 59
Noah Avatar answered Dec 25 '22 03:12

Noah


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();
like image 32
ruslanys Avatar answered Dec 25 '22 01:12

ruslanys