robert le
robert le

Reputation: 45

notifyDataSetChanged() not showing up for custom list adapter

link
I have a custom array adapter than extends ArrayAdapter, but the notifyDataSetChanged() method just plain doesn't show up(and does not work if you try it).

class matchAdapter extends ArrayAdapter<Match> {
public matchAdapter(Context context, List<Match> matches) {
    super(context, R.layout.match_layout, matches);
}
@Override
public void notifyDataSetChanged() {
    super.notifyDataSetChanged();
}

The above is from my adapter class. Would notifyAll() do the same thing?

Upvotes: 2

Views: 987

Answers (3)

N.T.
N.T.

Reputation: 2611

Your listAdapter is declared as having the type ListAdapter, which doesn't have the method you expect (notifyDataSetChanged). You need to make it at least BaseAdapter or cast it to a BaseAdapter(I am assuming your MatchAdapter is at least a BaseAdapter - if not, well, calling such a method would make no sense:-) )

((BaseAdapter)listAdapter).notifyDataSetChanged();

Upvotes: 1

Ryan Cole
Ryan Cole

Reputation: 100

Have you tried compiling and running the app? If there are any syntax errors above your matchAdapter (like a missing ; or "), it'll screw the autocompletion up.

Upvotes: 0

Skizo-ozᴉʞS ツ
Skizo-ozᴉʞS ツ

Reputation: 20616

I'll write an example, it's not a real code but it's how you have to do this.

  1. Create your List

    ArrayList<matchList> matchList = new ArrayList<matchList>();

  2. Create an Adapter

    adapter = new matchAdapter(this, matchList);

  3. Set the Adapter

    YourListView.setAdapter(adapter);

  4. You change the stuff on your List

    matchList.add(Stuff for your list);

  5. Notify now that List has changed doing :

    adapter.notifyDataSetChanged();

Hope it helps to you, if it does not, just type a comment and I'll try to update my answer.

Edit

I'll give you some examples...

When you want to call notifyDataSetChanged() just do this :

List.clear();
List.addAll(YourStuff);
adapter.notifyDataSetChanged();

Other method could be, you are using an AsyncTask so your onBackGround() you could make it returns items for your list, then on your onPostExecute update the adapter.

Example :

 @Override
  protected List<matchList> doInBackground(String... params) {
       List<matchList> items = loadUpdatedDataset(params);
       return items;
  }      

  @Override
  protected void onPostExecute(List<matchList> itemschangeds) {
    updateAdapterDataset(itemschangeds);
    adapter.notifyDataSetChanged();
  }

Upvotes: 0

Related Questions