Listview项目点击次数

时间:2017-05-13 08:29:34

标签: android

我有一些listview有一些项目。我想知道列表项click.for例如,如果我点击列表视图项目第一次一个toastt消息将出现。相同的项目点击吐司消息将是你点击了相同的项目第二次。

最后我想知道单个列表视图项“偶数”点击和“ODD”点击

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;

        }
    }
});

1 个答案:

答案 0 :(得分:1)

您可以添加一个额外的ArrayList,它将包含每个列表视图项的计数器。

假设你有:

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);
   }


}});