android – What's Adapter.getItem() for?-ThrowExceptions

Exception or error:

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?

How to solve:

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.

Leave a Reply

Your email address will not be published. Required fields are marked *