Android 列表视图刷新 [英] Android List view refresh
问题描述
我有一个 ListView,它通过一个数组向我显示一些数据(它在另一个类中,我通过它的对象访问它).
I have one ListView which is showing me some data through an array (which is in another class and I'm accessing it through it's object).
每当我通过上下文菜单从 ListView 中删除一个元素时,列表不会刷新,但元素会从数组中删除.如何刷新列表以显示此内容?
Whenever I delete an element from the ListView through context menu, the list is not refreshing but the element is deleted from the array. How can I refresh the list to show this?
代码:
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
if (v.getId()==R.id.mainListView) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
post=info.position;
menu.setHeaderTitle(stocks[info.position]);
String[] menuItems = stt;
for (int i = 0; i<menuItems.length; i++) {
menu.add(Menu.NONE, i, i, menuItems[i]);
}
}
}
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
int menuItemIndex = item.getItemId();
String[] menuItems = stt;
String menuItemName = menuItems[menuItemIndex];
listItemName = stocks[info.position];
stockname.remove(post-1);
return true;
}
推荐答案
您必须通知您的 ListView 适配器数据已更改.
You have to notify your ListView adapter that the data has changed.
listViewAdapater.notifyDataSetChanged();
如果由于某种原因这不起作用,并且在某些奇怪的情况下似乎没有通知,您可以通过构造函数使用更新后的数组重新分配适配器.
If that for some reason doesn't work and there are some wierd situations where it seems like it wasn't notifying, you can just reassign your adapter via the constructor with the updated array.
这篇关于Android 列表视图刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!