Android 列表视图刷新
我有一个 ListView,它通过数组向我显示一些数据(该数组位于另一个类中,我通过它的对象访问它)。
每当我通过上下文菜单从 ListView 中删除元素时,列表不会刷新,但该元素会从数组中删除。我怎样才能刷新列表来显示这一点?
代码:
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;
}
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).
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?
Code:
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您必须通知您的 ListView 适配器数据已更改。
如果由于某种原因不起作用,并且在一些奇怪的情况下似乎没有通知,您可以通过构造函数使用更新的数组重新分配适配器。
You have to notify your ListView adapter that the data has changed.
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.
我没有
notifyDataSetChanged()
方法(Android 2.3)。以下内容对我有用:
就我而言,我已经更改了整个适配器,但它仍然没有刷新,所以对我来说,上述方法是唯一有效的解决方案。
I have no
notifyDataSetChanged()
method (Android 2.3).The following worked for me:
In my case I have changed the whole adapter and it still didn't refresh, so for me the method described above is the only working solution.
尝试 getListView()。refreshDrawableState
Try getListView().refreshDrawableState
}
}
对我来说列表刷新:
listView.invalidateViews();
它会做:
管理某些列表项删除或一次删除所有元素的情况。
在 API 24 中测试。
List refreshing for me:
listView.invalidateViews();
It'll do:
The case as to manage some list item removal or removal all element at once.
Tested in API 24.