What is the difference between BaseAdapter and ArrayAdapter?

I want to know the difference between using BaseAdapter and ArrayAdapter .

I got what I want through ArrayAdapters .

Does this affect the performance of the ListView on the adapter interface in which it is implemented?

And the last question: can I do something with ListView using any of these Adapters , or are there certain cases when a particular adapter can only be used?

+97
performance android listview android-arrayadapter baseadapter
May 28 '13 at 15:43
source share
6 answers

Here is the difference:

  • BaseAdapter is a very general adapter that allows you to do anything you want. However, you have to do a bit more coding to make it work.
  • ArrayAdapter is a more complete implementation that works well for data in arrays or ArrayList s. Similarly, there is a related CursorAdapter that you should use if your data is in Cursor . Both of them are expanded by BaseAdapter .

If your data is in a specialized collection of some type or if you do not want the default behavior that the ArrayAdapter provides, you will most likely want to extend the BaseAdapter to get the flexibility you need.

The performance of each one really depends on how you implement them or change your behavior. At its core, any of them can be just as effective (especially considering that the ArrayAdapter is a BaseAdapter ).

You can do anything with any adapter, but keep in mind that the BaseAdapter is abstract, so you cannot use it directly.

+107
May 28 '13 at 15:50
source share

BaseAdapter is abstract, and the ArrayAdapter extends the BaseAdapter (this is a concrete implementation of the BaseAdapter ). If you extend the ArrayAdapter you inherit all the functions of the ArrayAdapter and, overriding its implementation, you can change the behavior of the ArrayAdapter . If you extend the BaseAdapter you must implement all the Abstract methods that the ArrayAdapter already implements.

Does it also affect ListView performance?

no.

And the last question: can I achieve anything using ListView, using any of these adapters, or in certain cases can I use only a specific adapter?

If the SDK implementation meets your needs, why would you redefine it and reinvent the wheel?

The main difference between the two is that the BaseAdapter cannot be an ArrayAdapter and the ArrayAdapter can

+19
May 28 '13 at 15:51
source share

In answer to your 3 questions:

(1) The BaseAdapter , according to the Android documentation, is simply a superclass of several types of adapters, one of which is the ArrayAdapter . There are a number of other BaseAdapter based BaseAdapter that are suitable for a variety of purposes. As a result, there is no difference in performance between them; you just get access to another set of functions / methods with different subclasses.

(2) The effectiveness of your ArrayAdapter depends on the efficiency of what you do inside this class, that is, processing bitmap images and other data.

(3) Perhaps you could find a way to work with your ListView using a different type of adapter, however, the reason the ArrayAdapter works is that it usually makes sense, given that the goal is to create an interactive list. ArrayAdapte takes an Array , usually an ArrayList objects, which it then processes to create supporting information for the ListView . In this sense, setting ArrayList ArrayAdapter ListView just logically makes sense.

+9
May 28 '13 at 15:57
source share

Another difference between BaseAdapter and ArrayAdapters is that if you extend the array adapter, you must call the class superclass in the constructor of the subclass.

 UserListAdapter extends ArrayAdapter<String>{ List<String> UserList; Context context; public UserListAdapter(Context context, int resource,List<String> listUsers) { super(context, resource, listUsers); /* Super class constructor is called */ UserList = listUsers; this.context = context; } } 

But there is no superclass for BaseAdapter. Because the BaseAdapter acts as a superclass for all other adapters

 UserListAdapter extends BaseAdapter{ List<String> UserList; Context context; public UserListAdapter(Context context, int resource,List<String> listUsers) { /* No super class constructor */ UserList = listUsers; this.context = context; } } 
+6
Mar 10 '15 at 8:51
source share
 private static final String[] COUNTRIES = new String[]{ "Afghanistan", "Angola", "Australia", "Bangladesh", "Belgium", "Bhutan", "Brazil", "Canada", "China", "Denmark" }; ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_dropdown_item_1line, COUNTRIES); AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.countries_list); textView.setAdapter(adapter); 

Here we cannot use a BaseAdapter such as an ArrayAdapter.

+4
Sep 30 '14 at 13:41
source share

BaseAdapter are more minimal and customizable. ArrayAdapter implement the most common uses of ListAdapter s, but sometimes they are not enough.

However, almost anything can be achieved using ArrayAdapter s. If your lists already work using the ArrayAdapter correctly, you have nothing to gain from BaseAdapter.

However, if you save your Context , Arrays or Lists in your ArrayAdapter implementation, you might want to switch to BaseAdapter , since the ArrayAdapter has already implemented them.

In any case, if you do this, you can always remove these elements and resort to using getItem() to get the elements of the array, getCount() to get the size of the array, and getContext() to inflate your views.

+3
May 28 '13 at 15:49
source share



All Articles