I know that ListAdapter is an interface and ArrayAdapter is a class. So we can only instantiate ArrayAdapter. I met up a code
ListAdapter adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, songNames);
But I was able to do the same thing with
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, songsArray);
So I want to know, in what exact places do we need ListAdapter ?
Thank you
Here is the difference: BaseAdapter is a very generic adapter that allows you to do pretty much whatever you want. However, you have to do a bit more coding yourself to get it working. ArrayAdapter is a more complete implementation that works well for data in arrays or ArrayList s.
android.widget.ArrayAdapter<T> You can use this adapter to provide views for an AdapterView , Returns a view for each object in a collection of data objects you provide, and can be used with list-based user interface widgets such as ListView or Spinner .
notifyDataSetChanged. Notifies the attached observers that the underlying data has been changed and any View reflecting the data set should refresh itself.
What is an 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.
This is not related to Android. But a general Java question.
When you use ListAdapter
as the type for your variable, you are really interested in the interface. You may not be interested in calling some specific method of ArrayAdapter<String>
not available in ListAdapter
. That is mainly because you will just assign the adapter as the list adapter for the view.
You may use the precise type of ArrayAdapter<String>
if you really need to call some specific methods.
It all depends on your use case.
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