adapter.notifyDataSetChange() not working after called from onResume()

China☆狼群 提交于 2019-12-13 04:47:55

问题


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?


回答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);


来源:https://stackoverflow.com/questions/13444598/adapter-notifydatasetchange-not-working-after-called-from-onresume

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