0

I am having a problem where when I call the adapter.notifyDataSetChange() to refresh the listActifity from the onResume() function, it doesn't seem to be working from any other functions from that activity afterwards.

I want the list(view) to refresh when the user clicks the back button(while on another screen) and returns to the window with the list. One of the things I noticed is that the notifyDataSetChange() works(from other functions) when I change one of the objects from the array list but not when I want to add or delete an object from the ArrayList. This has been working so far for me, but I would prefer not to have to requery the list every time.

@Override  
        protected void onResume() {  
        lightWeightDAO.open(); //db connection  
        adapter.clear();  
        buckets = lightWeightDAO.getExerciseBucketsByWorkoutId(workout.getId());   
        adapter.addAll(buckets);  
        adapter.notifyDataSetChanged();  
        super.onResume();      
    }

When I remove the notifyDataSetChange() from the onResume(), everything seems to work(just calling a simple notifyDataSetChange() after changing the arraylist).

Any idea why this is not working?

Ben Pearson
  • 7,532
  • 4
  • 30
  • 50
user1325843
  • 199
  • 2
  • 7
  • use "{}" button for code – Marcin Orlowski Nov 18 '12 at 21:09
  • As a note most adapters' `add()`, `addAll()`, etc methods call `notifyDataSetChanged()` for you. What data types are `adapter` and `buckets`? – Sam Nov 18 '12 at 21:15
  • adapter is an ExerciseBucketArrayAdapter that extends ArrayAdapter, buckets is an ArrayList where ExerciseBucket is just a serialiazable data class with getters and setters. Hope this helps. – user1325843 Nov 18 '12 at 22:34
  • Call super.onResume() as first method in your overriden onResume – Herr K Nov 18 '12 at 22:36
  • 1
    How do you add data in the other functions: `buckets.add()` or `adapter.add()`? (To reply to a specific user use `@Sam` otherwise I might not notice you replied.) – Sam Nov 18 '12 at 22:52
  • @Sam I use 'buckets.add()' to add the buckets to the 'ArrayList()'. – user1325843 Nov 19 '12 at 03:41
  • @Herr K placing 'super.onResume()' before the code did not change the behavior. – user1325843 Nov 19 '12 at 03:42

1 Answers1

1

By using:

buckets = lightWeightDAO.getExerciseBucketsByWorkoutId(workout.getId());   
adapter.addAll(buckets);  

You have only added the contents of this new buckets to the adapter, you didn't bind the adapter to buckets. So this:

buckets.add(string);
adapter.notifyDataSetChanged();

has no affect on the data inside the adapter. Also like I mentioned above in the comments, you can add to the adapter directly and it will call notifyDataSetChanged() for you. So simply replace everywhere you use the two lines above with:

adapter.add(string);
Sam
  • 86,580
  • 20
  • 181
  • 179