gpt4 book ai didi

java - 单击 3 次后删除 ListView 项目

转载 作者:行者123 更新时间:2023-12-02 02:43:13 25 4
gpt4 key购买 nike

如何从 Android 上的 ListView 中删除 ListView 项目?
但是,我想在删除每个项目之前将其点击次数设置为 3。
因此,如果第一个位置的项目被单击一次,第二个项目被单击两次,则在第一个项目点击达到 3 之前不要删除任何项目。然后仅删除该项目,对于 ListView 中的其他项目,每个项目都必须单击 3次。

listi.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,final int position, long id) {
final PopupMenu pop = new PopupMenu(Danger.this, listi);
pop.getMenuInflater().inflate(R.menu.menu_location, pop.getMenu());
pop.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {

case R.id.Remove:


items.remove(position);

}//swithc
return false;

最佳答案

创建一个整数ArrayList,并使用与 ListView 相同的元素计数对其进行初始化,并将列表中所有元素的值设置为= 0

ArrayList<integers> counterList = new Arraylist();
for(int i = 0; i < listi.getAdapter.getChildrenCount(); i++){ // get total elements in adapter
counterList.add(0); // set each element of array list to 0
}

然后这里:

listi.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,final int position, long id) {
final PopupMenu pop = new PopupMenu(Danger.this, listi);
pop.getMenuInflater().inflate(R.menu.menu_location, pop.getMenu());
pop.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {

case R.id.Remove:
if(counterList.get(position) >= 2){

items.remove(position); // remove current position item from arraylist adapter and notify data set changed
counterList.remove(position); // remove the current position element from counter list too
} else {
counterList.set(position, counterList.get(position) + 1); // if 3 clicks have not happened then increase the counter.
}

}//swithc
return false;

关于java - 单击 3 次后删除 ListView 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45079382/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com