Here is an example I of a custom adapter I used in a list view in which the user can select items in the listView and mark them for Delete. The key is to add the onClickListener after you have a view. Then you can use that to not only change the view, but update the data of the adapter too.
Hopefully you can modify this code to suit your particulars.
private class DeletePlayerAdapter extends ArrayAdapter<Player> {
Context context;
int layoutResourceId;
ArrayList<Player> data;
public DeletePlayerAdapter(Context context, int layout,
ArrayList<Player> list) {
super(context, layout, list);
this.layoutResourceId = layout;
this.context = context;
this.data = list;
}
@Override
public View getView(final int position, View convertView,
ViewGroup parent) {
View row = convertView;
PlayerHolder holder = null;
if (row == null) {
LayoutInflater inflater = ((Activity) context)
.getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new PlayerHolder();
holder.player_name = (TextView) row
.findViewById(R.id.player_name);
holder.player_number = (TextView) row
.findViewById(R.id.player_number);
holder.seeded_button = (ImageButton) row
.findViewById(R.id.delete_toggle);
holder.player_name.setTypeface(tf);
holder.player_number.setTypeface(tf);
row.setTag(holder);
players_array.get(position).marked_for_delete = false;
} else {
Log.d("PLAYER_ADAPTER", "NOT_NULL ROW");
holder = (PlayerHolder) row.getTag();
}
holder.seeded_button.setOnClickListener(new OnClickListener() {
private int pos = position;
public void onClick(View v) {
ImageButton b = (ImageButton) v;
if (b.isSelected()) {
b.setSelected(false);
players_array.get(pos).marked_for_delete = false;
} else {
b.setSelected(true);
players_array.get(pos).marked_for_delete = true;
}
}
});
Player p = data.get(position);
holder.player_name.setText(p.name);
holder.player_number.setText(String.valueOf(position+1));
holder.seeded_button
.setSelected(players_array.get(position).marked_for_delete);
return row;
}
}
static class PlayerHolder {
TextView player_number;
TextView player_name;
ImageButton seeded_button;
}