Question

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?

Was it helpful?

Solution

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();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top