gpt4 book ai didi

android - 单击按钮后重置计数器

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:56:00 24 4
gpt4 key购买 nike

DeleteTask 中,我有一个按钮用于在 listView 中选中复选框时删除列表。

delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

int itemCount = listview.getCount();
for (int i = itemCount - 1; i >= 0; i--) {
SearchList search = adapter.getItem(i);
if (search.isSelected()) {
adapter.removeItem(i);
delete.setText("DELETE");
counter=0;
}
}
}
});

DeleteAdapter中,它有一个计数器,用于对选中的框进行计数,并在按钮中显示该计数器。单击删除 button 后,计数器假设重置为 0,并且仅在 delete button setText 中显示 DELETE

 holder.ckbox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (((CheckBox) v).isChecked()) {
int getPosition = (Integer) v.getTag(); // Here we get the position that we have set for the checkbox using setTag.
search.get(getPosition).setSelected(((CheckBox)v).isChecked()); // Set the value of checkbox to maintain its state.
checkBoxCounter ++;
delete.setText("DELETE"+""+"("+ checkBoxCounter +")");
} else
{
int getPosition = (Integer) v.getTag(); // Here we get the position that we have set for the checkbox using setTag.
search.get(getPosition).setSelected(((CheckBox) v).isChecked()); // Set the value of checkbox to maintain its state.
checkBoxCounter--;
if (checkBoxCounter == 0) {
delete.setText("DELETE");
}
else {
delete.setText("DELETE" + "" + "(" + checkBoxCounter + ")");
}
}
}
});

我现在的问题是单击删除按钮时,它显示“DELETE”,但是当我选中复选框时,计数器没有重置。如何重置计数器?

编辑

我已将计数器更改为 checkBoxCounter,但仍然得到相同的结果!

 delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

int itemCount = listview.getCount();
for (int i = itemCount - 1; i >= 0; i--) {
SearchList search = adapter.getItem(i);
if (search.isSelected()) {
adapter.removeItem(i);
delete.setText("DELETE");
checkBoxCounter=0;
}
}
}
});

When I click button delete, the button shows DELETE. But, when I press another checkbox again, the counter starts from the last set value instead of 1.

请帮忙。

这是我的应用程序屏幕截图的一部分

/image/R954N.jpg

最佳答案

如果有两个不同的类,则在适配器类中添加一个方法来重置计数器。

可能是这样的:

  1. 在适配器类中,添加:

    public void resetCheckedCounter(){
    checkBoxCounter = 0;
    }
  2. 对于“删除”按钮,添加:

    adapter.resetCheckedCounter();

希望对您有所帮助!

关于android - 单击按钮后重置计数器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40738685/

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