Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between ArrayAdapter and ListAdapter in Android?

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

like image 636
AnujAroshA Avatar asked Nov 14 '11 15:11

AnujAroshA


People also ask

What is difference between ArrayAdapter and BaseAdapter in Android?

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.

What is an ArrayAdapter in Android?

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 .

What is the use of notifyDataSetChanged in Android?

notifyDataSetChanged. Notifies the attached observers that the underlying data has been changed and any View reflecting the data set should refresh itself.

What is custom adapter in android?

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.


1 Answers

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.

like image 198
Pierre Avatar answered Oct 13 '22 00:10

Pierre