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
f开发者_开发问答or 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?
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.
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()
.
精彩评论