Android onResume更新列表适配器
我正在使用列表适配器来显示不同的商店,当有人选择商店时,会将他们带到一个新活动,他们可以在该屏幕上将商店添加到收藏夹。
上面有一个“后退”按钮,可以调用 finish();
返回到列表视图屏幕。
现在的问题是列表视图未更新(即不显示商店已添加到收藏夹)。我尝试了这段代码,但没有成功:
@Override
public void onResume() {
super.onResume();
list.setAdapter(null);
updateMyList();
adapter=new LazyAdapter(this, ((String[])names.toArray(new String[0])),
((String[])status.toArray(new String[0])));
list.setAdapter(adapter);
}
updateMyList()
调用服务器 API 并更新名称和状态数组。
使用此代码,列表并没有真正更新......
I'm using a list adapter to show different stores, when someone selects a store it takes them to a new activity where they can add the store to favorite on that screen.
There is a Back button on that calls finish();
that goes back to the screen with the listview.
Now the problem is the listview isn't updated (ie. doesn't show that the store is added to favorite already). I tried this code but no luck:
@Override
public void onResume() {
super.onResume();
list.setAdapter(null);
updateMyList();
adapter=new LazyAdapter(this, ((String[])names.toArray(new String[0])),
((String[])status.toArray(new String[0])));
list.setAdapter(adapter);
}
updateMyList()
calls the server API and updates the names and status arrays.
With this code the list doesn't really update...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您应该仅在
onCreate()
中设置Adapter,在onResume()
内,您只需使用新的集合调用adapter.notifyDataSetChanged()
数据。这将使用新的数据集合刷新您的 ListView。You should setAdapter in your
onCreate()
only, insideonResume()
you just had to calladapter.notifyDataSetChanged()
with the new collection of data. This will refresh your ListView with the new collection of data.使用此代码:
我认为这会对您有所帮助。
谢谢....
Use this code:
I think this will help you.
Thanks....
首先,您需要将商店列表添加到数组列表中,
将此数组列表提交给ADAPTER,然后将其添加到列表视图中,
它将显示商店列表;采取
onListItemClick
点击那里你会得到listItem
Id;通过使用列表项 ID,您可以给出这样的意图:采用 java bean /setter 和 getter 方法类;将 getter 方法设为静态,设置您在子 Activity 中创建的商店名称;重写名为
onBackPressed()
的方法;在该方法内部写入父类中的方法
onActivityForResult()
;在该方法内部将该数组列表添加到适配器并编写代码
First of all u need to add the list of stores to the arraylist,
submit this array list to the ADAPTER then add it to the list view
it will display the stores list; take
onListItemClick
click there u will getlistItem
Id; by using the list item ID u can give intent like thisTake a java bean /setter and getter methods class; take getter method as static, set the store name what you have created in the child activity; override method called
onBackPressed()
; inside of that method writetake a method
onActivityForResult()
in the parent class; inside of that methodadd that arraylist to the
Adapter
and write the code在将新视图添加到 listView 之前,请删除所有先前的视图
,然后添加具有新值的适配器类。一件事是确保当您从第二个活动返回时更新您的值,并且此代码正在调用。
Before adding new View to listView remove all previous view
and then add adapter class with new values. One thing make sure that your values are updated while you returning from second activity, and this code is calling.