Here's my getView
function in a custom class extending ArrayAdapter
.
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
Thing p = getItem(position);
Thing.ThingStatus thingStatus = p.getStatus();
int thingStatusIcon; // change to thingStatusResource
switch (thingStatus) {
case A:
thingStatusIcon = ICON_A;
break;
case B:
thingStatusIcon = ICON_B;
break;
default:
thingStatusIcon = ICON_C;
break;
}
// Check if an existing view is being reused, otherwise inflate the view
ThingRowHolder viewHolder; // view lookup cache stored in tag
if (convertView == null) {
// If there's no view to re-use, inflate a brand new view for row
viewHolder = new ThingRowHolder();
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.thing_list_item, parent, false);
// put these on the top like the colors
viewHolder.thingIcon = (ImageView) convertView.findViewById(R.id.thing_icon);
viewHolder.thingId = (TextView) convertView.findViewById(R.id.thing_id_item);
viewHolder.thingStatus = (TextView) convertView.findViewById(R.id.thing_status_item);
// Cache the viewHolder object inside the fresh view
convertView.setTag(viewHolder);
} else {
// View is being recycled, retrieve the viewHolder object from tag
viewHolder = (ThingRowHolder) convertView.getTag();
}
viewHolder.thingId.setText(p.getLpId());
viewHolder.thingStatus.setText(thingStatus.toString());
viewHolder.thingIcon.setImageResource(thingStatusIcon);
viewHolder.position = position;
return convertView;
}
I'm trying to add an onClick
event listener for each row in my ListView
. I tried setting the listener on convertView
both inside the if
statement and outside the else
statement. I got mixed results. Sometimes the onClick
wouldn't fire at all. Other times it would report the wrong position. I've scoured SO for examples of this and I'm surprised such a common functionality isn't well documented and/or not working in my case.
Any help would be much appreciated!
edit:
private class ThingRowHolder {
ImageView thingIcon;
TextView thingId;
TextView thingStatus;
}
edit 2:
private void refreshList() {
ArrayList<Thing> things = mThingContainer.getThings();
Collections.sort(things);
mThingListAdapter.refreshThings(things);
}
edit 3:
mThingListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ThingListAdapter.ThingRowHolder holder =
(ThingListAdapter.ThingRowHolder) view.getTag();
Thing p = mThingContainer.getThings().get(holder.position);
}
});