What is the intent of the methods getItem and getItemId in the Android class BaseAdapter? What is the intent of the methods getItem and getItemId in the Android class BaseAdapter? android android

What is the intent of the methods getItem and getItemId in the Android class BaseAdapter?


I see these methods as a cleaner approach to accessing my list's data. Instead of directly accessing my adapter object via something like myListData.get(position) i can simply call the adapter like adapter.get(position).

The same goes for getItemId. Usually I would use this method when I want to execute some task based on the unique ID of an object in the list. This is especially useful when working with a database. The returned id could be a reference to an object in the database which I then could perform different operations on(update/delete/etc).

So instead of accessing the ID from the raw data object like myListData.get(position).getId() you can use adapter.getItemId(position).

One example of where i've felt like I needed to use these methods was in a project using the SeparatedListViewAdapter. This adapter can contain multiple different kinds of adapters, each representing data of a different type(typically). When calling getItem(position) on the SeparatedListViewAdapter, the object returned may be different depending on which "section" the position is that you send it.

For example, if you had 2 sections in your list(fruit and candy): If you used getItem(position) and position happened to be on an item in the fruit section, you would receive a different object than if you requested getItem(position) with position pointing to an item in the candy section. You might then return some sort of constant ID value in getItemId(position) which represents what kind of data getItem(position) is returning, or use instanceof to determine what object you have.

Other than what I've mentioned, I've never felt like I really needed to use these methods


Well, it seems that this question could be answered in a simpler and more straightforward way... :-)

Simply put, Android allows you to attach a long to any ListView item, it's that simple. When the system notifies you of the user selection, you receive three identifying variables to tell you what was selected:

  • a reference to the view itself,
  • its numeric position in the list,
  • this long you attached to the individual elements.

It's up to you to decide which of these three is the easiest for you to handle in your particular case but you have all three to choose from all the time. Think of this long as a tag automatically attached to the item, only that it's even simpler and easier to read out.

The misunderstanding about what it usually does stems from a simple convention. All adapters have to provide a getItemId() even if they don't actually use this third identification. So, by convention, those adapters (including many in samples in the SDK or all around the web) simply return position for a single reason: it's always unique. Still, if an adapter returns position, this really means it doesn't want to use this feature at all, since position is already known, anyway.

So, if you need to return any other value you see fit, feel free to do so:

@Overridepublic long getItemId(int position) {  return data.get(position).Id;}


The getItemId method is largely designed to work with Cursors that are backed by SQLite databases. It will return the underlying cursor's id field for the item in position 1.

In your case there isn't an id for the item in position 1: I'm assuming ArrayAdapter's implementation just returns -1 or 0.

EDIT: actually, it just returns the position: in this case 1.