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

I'm curious about the purpose of the methods getItem and getItemId in the class Adapter in the Android SDK.

From the description, it seems that getItem should return the underlying data. So, if I have an array of names ["cat","dog","red"] and I create an adapter a using that, then a.getItem(1) should return "dog", correct? What should a.getItemId(1) return?

If you've made use of these methods in practice, could you provide an example?


Solution 1:

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

Solution 2:

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:

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