I have placed in ArrayAdapter an ImageView, the list looks like this:
Name1 |ImageView|
Name2 |ImageView|
Name3 |ImageView|
end so on..
I don’t know how to get an item id after clicking ImageView.
@Override
public View getView(int position, View contextView, ViewGroup parent) {
final ViewHolder viewHolder;
View rowView = contextView;
final CardList list = _cardList.get(position);
if (rowView == null) {
LayoutInflater layoutInflater = _context.getLayoutInflater();
rowView = layoutInflater.inflate(R.layout.cardlist_viewlist_tpl, null, true);
viewHolder = new ViewHolder();
viewHolder.tvCardListName = (TextView) rowView.findViewById(R.id.tvCardListName);
viewHolder.btnCardListLearn = (ImageView) rowView.findViewById(R.id.btnCardListLearn);
viewHolder.btnCardListLearn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(_context, LearningCardActivity.class);
//!!!!!!!!!!!!!!!
//this doesn't work, I always get 1st positon Id
intent.putExtra(LearningCardActivity.CURRENT_CARD_LIST_ID, list.getId());
//!!!!!!!!!!!!!!!!
_context.startActivity(intent);
}
});
rowView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) rowView.getTag();
}
viewHolder.tvCardListName.setText(list.getName());
return rowView;
}
It seems like you’re re-using the
rowView/contextViewand that you only set your onClickListener if the rowView equalsnull. That basically means that the listener on yourviewholderobject will only be set the first timegetView()gets called. Hence you will always see the same first item’s id .If you want to set the listener for every row, you will need to change the logic and move the
setOnClickListener()call.