Android:如何从 listView 和 arrayAdapter 中删除项目 [英] Android: how to remove an item from a listView and arrayAdapter
问题描述
我在 ArrayList 中有一组项目.我将它们添加到客户适配器中,如下所示:
I have a collection of items in an ArrayList. I add them to a customer adapter as follows:
this.m_adapter = new MyAdapter(this, R.layout.myitem,
itemCart.m_items);
我的列表中的每个项目都有一个删除按钮,但我不确定如何将删除按钮的 onClick()
与 ArrayList 中的原始项目连接起来.有人可以解释一下如何做到这一点或向我指出一个教程,我可以在其中阅读吗?非常感谢非讽刺/非居高临下的回应.
I have a delete button for each of these items in my list, but I am not sure how to connect the delete button's onClick()
with the original item in the ArrayList. Can someone please explain how to do this or point me to a tutorial where I can read up on this? Non-sarcastic/non-condescending responses are greatly appreciated.
推荐答案
这是我目前的解决方案:
Here's my solution so far:
在 getView()
方法中,我做这样的事情:
In the getView()
method I do something like this:
deleteButton.setTag(position);
看起来 getTag()
返回一个对象.所以我首先将位置 int 转换为 Integer 对象.它似乎正在工作.
It looks like getTag()
returns an Object. So I converted the position int into an Integer object first. It appears to be working.
在 OnClickListener()
中,我执行以下操作:
In the OnClickListener()
I do the following:
items.remove(index.intValue());
到目前为止,一切都很好.
So far, so good.
这篇关于Android:如何从 listView 和 arrayAdapter 中删除项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!