从列表视图中删除所选项目
我开发了一个代码,在其中动态填充列表视图。
现在我想在单击按钮时从列表视图中删除所选项目(按删除按钮)
我已在此网站中搜索到此内容,但没有得到任何确切的解决方案,因此我发布此问题
请帮助我如何执行此操作:
onClickListener 上删除按钮的代码如下所示:
DeleteButton.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
if (idx >= 0) {
Log.v("Item index deleted", idx + "");
idx = OdrLst.getCheckedItemPosition();
String delete = (String) ((OdrLst.getAdapter())
.getItem(idx));
// Long deteteId = OdrLst.getAdapter().getItemId(idx);
Log.d("Item deleted", delete);
Log.d("adapter count before", adapter.getCount() + "");
Log.d("lv count before", OdrLst.getCount() + "");
// Log.d("listitems count before", listItems.+"");
adapter.remove(delete);
//listItems.remove(idx);
adapter.notifyDataSetChanged();
OdrLst.setAdapter(adapter);
// OdrLst.removeViewAt(idx);
// adapter.clear();
Log.d("adapter count after", adapter.getCount() + "");
Log.d("lv count after", OdrLst.getCount() + "");
//adapter.notifyDataSetChanged();
// Log.v("adapter count after 1", adapter.getCount()+"");
}
// cleared = false; // <--- nope, we did not clear the value yet
// delItem();
}
});
此代码显示要删除的确切位置和项目,但该项目不会从列表视图中删除...
I have developed a code in which i have populated list view dynamically.
now i want to delete the selected item from list view on button click(on pressing delete button)
I have searched out this in this site but didn't got any exact solution so i am posting this question
please help me how to do this :
code on delete buttons onClickListener is as shown below :
DeleteButton.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
if (idx >= 0) {
Log.v("Item index deleted", idx + "");
idx = OdrLst.getCheckedItemPosition();
String delete = (String) ((OdrLst.getAdapter())
.getItem(idx));
// Long deteteId = OdrLst.getAdapter().getItemId(idx);
Log.d("Item deleted", delete);
Log.d("adapter count before", adapter.getCount() + "");
Log.d("lv count before", OdrLst.getCount() + "");
// Log.d("listitems count before", listItems.+"");
adapter.remove(delete);
//listItems.remove(idx);
adapter.notifyDataSetChanged();
OdrLst.setAdapter(adapter);
// OdrLst.removeViewAt(idx);
// adapter.clear();
Log.d("adapter count after", adapter.getCount() + "");
Log.d("lv count after", OdrLst.getCount() + "");
//adapter.notifyDataSetChanged();
// Log.v("adapter count after 1", adapter.getCount()+"");
}
// cleared = false; // <--- nope, we did not clear the value yet
// delItem();
}
});
This code shows exact position and item to be deleted but the item not gets removed from the listview...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
删除该项目后尝试添加此内容。
Try adding this after removing the item.
您可以制作一个包含复选框或图像视图的自定义列表视图,然后使用 Arraylist 获取列表中单击的项目。
参考这些链接:
从 Android 中的列表视图中删除项目
You can make a customized Listview containing check boxes or imageview and then use Arraylist to get the items which were clicked in the list.
refer these link:
Remove item from the listview in Android