Get listview item clicked count in android

2020-05-07 14:50发布

I want to count the number of clicks of particular item in list view. Suppose there are 3 items in list view and I click 1st item for 1st time, it should display toast message that 1st item clicked count is "1". Now, if I again click 1st item, message should be 1st item clicked count is "2" and so on for other items. How can I do this?

1条回答
SAY GOODBYE
2楼-- · 2020-05-07 15:24

Simply you can use Map<String, Integer> here to get the count for your ListView item. Just keep the default value as 0 in the Map and add 1 always inside the onItemClick() of ListView.

Pseudo Code,

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                                                                    long arg3) {
    int count = 0;
    try {
        count = map.get(your_listview_value);
    } catch (Exception e) {
        e.printStackTrace();
    }
    map.put(your_listview_value, (count + 1));
    Toast.makeText(getBaseContext(), 
                             String.valueOf(count), Toast.LENGTH_LONG).show();
}
查看更多
登录 后发表回答