OnItemClick only one time for each item

I have an activity with a list view, the list view is created in list view adapter so in an other class. How can I set that a several action on onItemClick is only fired once for each list item? I want to change the image source of the list item that was clicked, how can I reach it from my activity, if it is set in the listView Adapter?

Here is some code from the List View Adapter where the Items are set that are in the ListView:

 @Override
public View getView(final int position, View convertView, ViewGroup parent) {
    final ListCell cell;
    if (convertView == null) {
        convertView = inflater.inflate(R.layout.get_all_entry_list_view_cell, null);

        cell = new ListCell();
        cell.note = (TextView) convertView.findViewById(R.id.listViewNote);
        cell.img = (ImageView) convertView.findViewById(R.id.listViewImg);

        convertView.setTag(cell);

    }
    else {
        cell = (ListCell)convertView.getTag();
    }

 //.....Content is set with JSONObject from databse



 public class ListCell {
    private TextView note;
    private ImageView img;
}

Add a boolean field to your ListCell class so you can identify when an item has been touched. You will need to write appropriate getters and setters.

 public class ListCell {
    private TextView note;
    private ImageView img;
    private boolean touched;
}

Wherever you have your onItemClick() or onTouchEvent() or whatever method you override to handle the clicks, you can have some logic like this in it:

if(!listCell.touched) {
  //do whatever needs to happen when the item is clicked
  listCell.setTouched(true);
}

At some point you will also want to reset the boolean on all your list items if you want to allow users to click them again later, so just remember to do something like listCell.setTouched(false); before you allow users to click the items again.

链接地址: http://www.djcxy.com/p/30560.html

上一篇: 滚动listView时,ListView Items的背景会混淆

下一篇: OnItemClick每个项目只有一次