gpt4 book ai didi

android - ListView 项点击次数

转载 作者:行者123 更新时间:2023-11-30 00:32:59 24 4
gpt4 key购买 nike

我有一个包含一些项目的 ListView 。我想知道列表项目的点击情况。例如,如果我第一次点击 ListView 项目,就会出现一条 toast 消息。再次点击同一个项目, toast 消息将是你点击了第二次相同的项目。

最后我想知道单个 ListView 项目的“偶数”点击和“奇数”点击

listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
{ public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(firstTime) {
firstTime = false;
if(count%2==0){
Toast.makeText(getApplicationContext(), "Clicked on Row2nd: " +
country.getName(), Toast.LENGTH_LONG).show();
count=0;
}
else{
Toast.makeText(getApplicationContext(), "Clicked on Row1st: " +
country.getName(), Toast.LENGTH_LONG).show();
count=0;
}
}
else{
firstTime=true;
Toast.makeText(getApplicationContext(), "Clicked on Row1111: " +
country.getName(), Toast.LENGTH_LONG).show();
count=count+1;

}
}
});

最佳答案

您可以添加一个额外的 ArrayList,它将为您的每个 ListView 项目包含一个计数器。

假设您有:

ArrayList<String> listViewData = getSomeData();
ArrayList<Integer> dataClickCount = new ArrayList<>();
for(String item : listViewData)
{
dataClickCount.add(0);
}

稍后在您的听众中:

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

if(dataClickCount.get(posion)%2==0){
Toast.makeText(getApplicationContext(), "Clicked on Row1nd, odd click: " + country.getName(), Toast.LENGTH_LONG).show();
dataClickCount.set(posion,1);
}
else{
Toast.makeText(getApplicationContext(), "Clicked on Row2st, even click: " + country.getName(), Toast.LENGTH_LONG).show();
dataClickCount.set(posion,0);
}


}});

关于android - ListView 项点击次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43950947/

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