从onResume()调用后adapter.notifyDataSetChange()不起作用 [英] adapter.notifyDataSetChange() not working after called from onResume()

查看:128
本文介绍了从onResume()调用后adapter.notifyDataSetChange()不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一个问题,当我调用adapter.notifyDataSetChange()来从onResume()函数刷新listActifity时,此后似乎无法在该活动的任何其他函数中工作.

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.

当用户单击后退"按钮(在另一个屏幕上)并返回带有列表的窗口时,我希望刷新列表(视图). 我注意到的一件事是,当我从数组列表中更改一个对象时,notifyDataSetChange()可以(通过其他功能)起作用,但是当我想要从ArrayList中添加或删除对象时却不能.到目前为止,这对我来说一直有效,但是我希望不必每次都重新查询列表.

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

当我从onResume()中删除notifyDataSetChange()时,一切似乎都可以正常工作(在更改arraylist之后只调用一个简单的notifyDataSetChange()).

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?

推荐答案

通过使用:

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

您仅将此新buckets的内容添加到了适配器,没有将适配器绑定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();

对适配器内部的数据没有影响.就像我上面在评论中提到的一样,您可以直接添加到适配器,它将为您调用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);

这篇关于从onResume()调用后adapter.notifyDataSetChange()不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆