What's the difference between BaseAdapter and ArrayAdapter?

54,298

Solution 1

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 ArrayLists. Similarly, there is a related CursorAdapter that you should use if your data is in a Cursor. Both of these extend BaseAdapter.

If your data is in a specialized collection of some sort or if you don't want the default behavior that ArrayAdapter provides, you will likely want to extend BaseAdapter to get the flexibility you need.

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

You can do pretty much whatever you want with any adapter, but keep in mind that BaseAdapter is abstract, so you can't use it directly.

Solution 2

BaseAdapter is abstract while ArrayAdapter extends BaseAdapter (it is a concrete implementation of a BaseAdapter). If you extends for ArrayAdapter you are inheriting all the ArrayAdapter's features and, override its implementation you can modify the ArrayAdapter behavior. If you extends BaseAdapter you must implements all the Abstract methods, that ArrayAdapter already implements.

Also, does it affects the performance of the ListView

no it does not.

And, the last question is, can i achieve anything doing with ListView using any of these Adapters, or, there are certain cases where specific adapter only can be used ?

If the implementation in the SDK fulfill your needs why you need to override it and re-invent the wheel?

The main difference between them is that BaseAdapter can not be instantiate while ArrayAdapter can

Solution 3

In response to your 3 questions:

(1) BaseAdapter, according to the Android docs, is just a superclass of a number of adapter types, one of which is ArrayAdapter. There are a number of other adapters that are derived from BaseAdapter that fit different purposes. As a result it is unlikley there is any difference in efficiency between the two; you just get access to a different set of functions/methods with the various subclasses.

(2) The efficiency of your ArrayAdapter depends on the efficiency of what you're doing inside that class, i.e. the processing of bitmaps and other data.

(3) You could probably figure out a way to work your ListView using a different kind of adapter, however, the reason ArrayAdapter works is because it usually makes sense given the goal is to build an interactive list. The ArrayAdapte takes an Array, usually an ArrayList of objects, that it then processes to create the backing information for the ListView. In that sense the set up of ArrayList --> ArrayAdapter --> ListView just logically makes sense.

Solution 4

One more difference between BaseAdapter and ArrayAdapters is, if you extend array adapter you have to call super class constructor in the subclass constructor.

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 super class for BaseAdapter. As BaseAdapter serves as super class 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;
   }

}

Solution 5

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 can't use BaseAdapter like ArrayAdapter.

Share:
54,298

Related videos on Youtube

Chintan Soni
Author by

Chintan Soni

Hey visitor, Greetings !!! I am Chintan Soni, a Passionate, Tech savvy, Independent, Self-motivating Mobile Application Developer. Currently serving as TechLead - Android @ ARInspect. Always eager to learn new technologies. Love to help people learn with, whatever I got. Meet my brother: Yash Soni A tech-enthusiast, Gadget Freak and Apple Fanboy. Motto that LIT my life: Learn, Implement and Teach others. Github: https://github.com/iChintanSoni Medium: https://medium.com/@chintansoni My Mentor: Paresh Mayani, Pratik Patel Currently onto: Flutter Kotlin Platforms and Frameworks I have worked on: Android Native Application Development Ionic 3.x Application Development Microsoft ASP.net Web Api 2 using Entity Framework 6.1 (MSSQL Server Database 2014) Node.js (MySQL Server) Google Firebase Augmented Reality Facebook Messenger Bots And what not...!!! Follow me on: Facebook, Twitter, LinkedIn

Updated on December 16, 2020

Comments

  • Chintan Soni
    Chintan Soni over 3 years

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

    I have been achieving what I want through ArrayAdapters.

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

    And, the last question is, can i achieve anything doing with ListView using any of these Adapters, or, there are certain cases where specific adapter only can be used?

  • John
    John about 9 years
    And no need on DataType Specified in BaseAdapter like in ArrayAdapter. Example : ArrayAdapter<String>
  • Selvin
    Selvin almost 9 years
    also you should never use List<T> UserList; in the class which extends ArrayAdapter<T>
  • Felix
    Felix over 7 years
    The default behavior that ArrayAdapter provides (as mentioned by @tanis-7x ) is that there can be only 1 TextView inside the list item, actually very limited behavior. If there are multiple controls in each list item, you must use BaseAdapter and to the job by yourself. Of course you can still use ArrayAdapter, because ArrayAdapter is a BaseAdapter.