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?

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

What's Adapter.getItem() for?

If each item in the ListView represents some object, say a String, then you can return the String here. This is useful for your controller layer if it needs to be able to perform some logic when, eg. the user taps that item in the ListView.

Returning null is fine as the usage is up to you.

I've personally never returned anything but null here.

RecyclerView and get ItemId

EDIT:

Here are two links that might help you solve your issue :

  • What is the intent of the methods getItem and getItemId in the Android class BaseAdapter?
  • how to get ID of the items in recyclerview

OP used : adapter.getItemId(viewHolder.getAdapterPosition()); instead of viewHolder.getItemId() to solve his problem.

Android adapter.getItem(position).getItemId() not working (The method getId() is undefined for the type Object)

You must cast the getItem(position) return value to a Countries object.

Change this:

    intent.putExtra("countryId", adapter.getItem(position).getId());

To this:

    Countries countries = (Countries)adapter.getItem(position);
intent.putExtra("countryId", countries.getId());

Update:

Actually, after reading @Tushar's comment, I think you should make names a VisaAdapter instead of an ArrayList<Countries>(). Then, your original line of code, above, should work as-is.



Related Topics



Leave a reply



Submit