【发布时间】:2015-12-04 13:29:56
【问题描述】:
我有数组列表
private ArrayList<Items> mArrayList
我已经在这个 Arraylist 中添加了项目并绑定到适配器。绑定后我得到以下输出:
position(Arraylist) item_name
0 abc
1 chf
2 tiy
3 trf
4 lkj
5 xyz
所以现在我有two buttons 1)Remove and 2) undo
在删除按钮单击事件中,我正在删除项目。所以我使用了以下代码:
mArrayList.remove(0);
mItemAdapter.notifyDataSetChanged();
所以它是从 0 位置删除的项目。
之后我的数据是
position(Arraylist) item_name
0 chf
1 tiy
2 trf
3 lkj
4 xyz
所以现在我在下面写了撤消最后一项的代码:
item tm = (HomeScreenProduct) Object;
mArrayList.add(0, tm);
mItemdapter.notifyDataSetChanged();
所以现在最后一项没有添加到第 0 个位置。
我想当我点击撤消按钮时,最后一个被删除的项目应该设置在第 0 个位置。
所以当我点击撤消时,我想要像这样的输出
position(Arraylist) item_name
0 abc
1 chf
2 tiy
3 trf
4 lkj
5 xyz
因为每次移除项目后位置都会改变。
很简单,我想将最后删除的项目设置为0th position
我该怎么做?
【问题讨论】:
-
mArrayList.add(0, tm);应该将其添加到 0 位置 -
但它非常适合最后一项。但如果我有很多物品,它就不起作用了。
-
请描述不工作
标签: android object arraylist android-arrayadapter