Android AdapterView Click Listener Parameters - position & id

后端 未结 2 1283
时光取名叫无心
时光取名叫无心 2021-01-15 16:54

I\'m setting a long click listener on a listview and I want to use the index of the clicked item to retrieve the corresponding object.

Method signature and paramete

2条回答
  •  情深已故
    2021-01-15 17:18

    Position and ID may be the same, but it really depends on the adapter you're using.

    Basically 2 methods in the adapter dictate what the ID will be - in the case of a SimpleCursorAdapter (and a quick look at the source code) it's the '_id' field from the query that created the cursor, but the methods in the Adapter that dictate the id parameter are:

    Adapter.getItemId(int) which allows the adapter to convert from position to an id for the object, and Adapter.hasStableIds() which allows the ListView (or anything using the Adapter to cache it) - although you can probably ignore hasStableIds().

    The id will be the returned value of Adapter.getItemId(int) so if you use an ArrayAdapter it will be the same as the position - a quick search of the ArrayAdapter source code shows it's using return position; to work out the id :)

    If you use a custom adapter, then it's completely up to you.

提交回复
热议问题