-1

OK, so I have a broadcast receiver get called and in the onReceive() method I update a gallery with a new adapter using static data, all done on the UI thread.

However, nothing changes. The old data is left in the gallery, but when I debug my code and take my time it updates after invalidate is called. Oh, BTW, the view is in a ViewFlipper, but I change to the proper view before creating the new adapter. So I have a race condition, is this normal? If it isn't, what should I do?

Below is a sample of what I'm talking about in the onRecieve().

onRecieve() {
    mFlipper.setDisplayedChild(0);
    mNavAdapter.addCategory(-1);
    mGalNav.setSelection(0);
    getCategoryProducts(-1);
}

void getCategoryProducts(int category) {
   mGalProducts.setAdapter(new DealCheckInAdapter(this,
   getCheckInProducts()));
   mGalProducts.invalidate();
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
L7ColWinters
  • 1,342
  • 1
  • 14
  • 31

1 Answers1

1

The invalidate() method is not supposed to do what I'm guessing you thinking it will do. You suppose to use:

mDealCheckInAdapter.notifyDataSetChanged(); 

This is the method which "refreshing" the listView adapter.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Tal Kanel
  • 10,475
  • 10
  • 60
  • 98
  • but if its a new adapter i shouldn't have to call notifydatasetchanged() – L7ColWinters Apr 10 '12 at 21:18
  • that's true only if you are doing it inside the onCreate() of the activity when you are doing it for the first time. otherwise it won't refresh the listView with your new adapter. my advice - don't create each time you have a new data - a new adapter, but only add/replace the data in the List the adapter representing, and call notifyDataSetChanged(). that's the right way doing this – Tal Kanel Apr 10 '12 at 21:26
  • my gallery needs to display data from more than one adapter. I tried keeping an instance and invalidating of it but that still doesn't behave properly. I don't need notifyDataSetChanged() because i'm not changing the data internally, just changing the adapter that the gallery uses for data – L7ColWinters Apr 10 '12 at 22:06
  • ok so you were right, i guess even if you don't change the data, and just change the adapter of the parent, that notifyDataSetChanged() must be called! – L7ColWinters Apr 10 '12 at 22:17
  • great. I'm glad that finally it helped you – Tal Kanel Apr 11 '12 at 05:16