AnujAroshA
AnujAroshA

Reputation: 4811

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

Upvotes: 17

Views: 26121

Answers (2)

Amit Walke
Amit Walke

Reputation: 293

BaseAdapter Its a very generic adapter that allows you to do pretty much whatever you want. It’s a base class of all adapter.

ArrayAdapter Its a more complete implementation that works well for data in arrays or ArrayLists. Similarly,there is a related CursorAdapter that you should use if your data is in a Cursor. Both of these extend BaseAdapter

Upvotes: -1

Pierre
Pierre

Reputation: 2866

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.

Upvotes: 27

Related Questions