我有一个使用自定义数组适配器填充的ListView。我想获得ListView中每个项目的总点击次数。如何为多个项目映射列表视图项目ID和该项目的点击次数?
答案 0 :(得分:1)
使用此代码..完全..硬编码整个代码:p
HashMap<Integer,Integer> map_positions_and_count = new HashMap<>();
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
for (int i = 0; i<listView.getAdapter().getCount()-1; i++)
{
if (i == position)
{
if (map_positions_and_count .get(position) !=null)
{
map_positions_and_count .put(position,map_positions_and_count .get(position)+1);
}
else {
map_positions_and_count .put(position,1);
}
}
}
// map.put(position,)
}
});
您可以在mainActivity中使用它 ..
答案 1 :(得分:0)
您可以在活动中尝试此操作,
ArrayList<Integer> arrayList=new ArrayList<>();//make this as global
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(arrayList.get(position)!=null) {
int a = arrayList.get(position);
arrayList.set(position, a+1);
}
else
arrayList.set(position,1);
}
});
答案 2 :(得分:0)
试试此代码 - &gt;
final HashMap<Integer, Integer> listItemCount = new HashMap<Integer, Integer>();
//Set an onItemClickListener
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (listItemCount.containsKey(position)){
listItemCount.put(position, listItemCount.get(position)+1);
}else {
listItemCount.put(position, 1);
}
}
});