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?
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With