Eugene
Eugene

Reputation: 60184

How to associate each item in ListView with id from database

I have a ListView object with Adapter extended from CursorAdapter. There are only (ListView l, View v, int position, long id) parameters in onListItemClick() method. I need to query my database with _id for currently chosen list's item. But I don't want the _id of the row from database to be shown in the list's row. So I created additional TextView in list_item.xml and made it invisible by setting its width and height to 0px. So on newView() I can associate the list's item with it's _id in database.

I find this approach to be ugly a bit. Might be there are any other more neat solution?

Upvotes: 1

Views: 1210

Answers (2)

Patrick Kafka
Patrick Kafka

Reputation: 9892

You can use this ViewHolder pattern which will let you set a custom object in the tag of the row. You can add the extra properties to the ViewHolder class (like _id) and set them when creating your ViewHolder objects. It will also help with performance of inflating and reusing views.

Upvotes: 2

inazaruk
inazaruk

Reputation: 74780

You can use View.setTag() to set associate custom object with any view you return from your custom CursorAdapter. And then access this tag from your onListItemClick().

Upvotes: 2

Related Questions