Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

People also ask

What is getItemId?

getItemId is any unique long value you can attach with the same item in the list. People generally return the position in the list.

What is notifyDataSetChanged in Android?

notifyDataSetChanged. Notifies the attached observers that the underlying data has been changed and any View reflecting the data set should refresh itself.

What is AdapterView?

AdapterView is a ViewGroup that displays items loaded into an adapter. The most common type of adapter comes from an array-based data source.


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:

@Override
public 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.


If you implement getItemIdcorrectly then it might be very useful.

Example :

You have a list of albums :

class Album{
     String coverUrl;
     String title;
}

And you implement getItemId like this :

@Override
public long getItemId(int position){
    Album album = mListOfAlbums.get(position);
    return (album.coverUrl + album.title).hashcode();
}

Now your item id depends on the values of coverUrl and title fields and if you change then and call notifyDataSetChanged() on your adapter, then the adapter will call getItemId() method of each element and update only thouse items which id has changed.

This is very useful if are doing some "heavy" operations in your getView().

BTW : if you want this to work, you need to make sure your hasStableIds() method returns false;


I would like to mention that after implementing getItem and getItemId you can use ListView.getItemAtPosition and ListView.getItemIdAtPosition to directly access you data, instead of going through the adapter. This may be particularly useful when implementing an onClick listener.