知道在android中的listview中点击了一个项目的次数

时间:2015-06-23 14:52:49

标签: android listview

有没有办法找到Android中第一次点击ListView中的项目?

3 个答案:

答案 0 :(得分:0)

使用false初始化一个arraylist,列出listView中有多少项,然后尝试以下

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if(arrayList.get(position) == false){ //Make sure you initialize your arraylist with false values (same amount as listView size)
            arrayList.set(position, true); //set true to an arrayList in the same index of the listView that was pressed
        }else{
            //Has been clicked before, do something
        }


        }
});

答案 1 :(得分:0)

这会对你有所帮助。

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

要了解列表视图的基本功能Check this

答案 2 :(得分:0)

如果需要,可以将此信息包含在模型包装器中,例如,如果显示Items列表,那么您将拥有包装器:

class ItemWrapper {
    Item mItem;
    boolean mIsClicked;
}

在这种情况下,当您点击项目时,您可以获得有关此项目是否已被点击的信息。

如果您需要可检查项目列表(复选框列表),这是正确的方法,因为这个项目是您逻辑的一部分,如果您选中了项目,您肯定可以回答。