Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's Adapter.getItem() for?

Tags:

I am writing a custom adapter for use with a ListView.

The Adapter interface includes a getItem() method which returns, according to the docs, an Object as

the data item associated with the specified position in the data set.

What's this object supposed to be? I can only imagine the ListView wants to call either toString or equals on it, since there's not much else you can do with a raw Object. But I don't have a convenient Object I can return, and in any event I'm overriding getView so the ListView has no need for a String from my dataset anyway.

Can I just return null or something else completely irrelevant?

like image 336
Graham Borland Avatar asked Nov 11 '10 11:11

Graham Borland


People also ask

What is the purpose of adapter in Android?

An Adapter object acts as a bridge between an AdapterView and the underlying data for that view. The Adapter provides access to the data items. The Adapter is also responsible for making a View for each item in the data set.

What can adapter be used for?

An adapter acts as a bridge between an AdapterView and the underlying data for that view. The adapter provides access to the data items and is responsible for creating a view for each item in the data set. Adapters are a smart way to connect a View with some kind of data source.

What does an array adapter do?

ArrayAdapter is an Android SDK class for adapting an array of objects as a datasource. Adapters are used by Android to treat a result set uniformly whether it's from a database, file, or in-memory objects so that it can be displayed in a UI element. The ArrayAdapter is useful for the latter.

What is custom adapter in Android?

In Android, Adapter is a bridge between UI component and data source that helps us to fill data in UI component. It holds the data and send the data to an Adapter view then view can takes the data from the adapter view and shows the data on different views like as ListView, GridView, Spinner etc.


1 Answers

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.

like image 177
mxcl Avatar answered Sep 21 '22 00:09

mxcl