Flutter Course (21 Blogs) Become a Certified Professional

Android Adapter Tutorial: What Are Adapters in Android

Last updated on Feb 28,2023 90K Views


Adapters in Android are a bridge between the Adapter View (e.g. ListView) and the underlying data for that view. This is a crucial concept in Android architecture and is also required for Android CertificationImagine what a world it would have been without Android Adapters!

Why Android Adapter?

Without Android Adapter, to implement the ListView functionality, you will need to:

  • Create a TextView within a ScrollView group.

  • Then you will have to implement pagination concept for the contents of the TextView.

  • You will also have to write additional code to identify the click event on a particular row in the TextView.

Quite cumbersome job! Isn’t it?

You may be asking why we need Pagination?

Imagine that you are creating an application that needs to display all your emails and the user will be able to scroll through them. I get around 100 emails on a daily basis and even if we consider 10 emails per day, the email data is going to be extremely huge. If you do not implement pagination, you will have major performance issues in retrieving all that data and showing it on the mobile screen. Thank God, we have adapters in Android and adapter views!

Below is a conceptual diagram which shows the high level working of the Android Adapter:

How Android Adapter works

Let us now understand the internal working of an Android Adapter and how it acts as a data pump to the adapter view.

Adapters call the getView() method which returns a view for each item within the adapter view. The layout format and the corresponding data for an item within the adapter view is set in the getView() method. Now, it will be a performance nightmare if getView() returns a new View every time it is called. Creating a new view is very expensive in Android as you will need to loop through the view hierarchy (using the find ViewbyID () method) and then inflate the view to finally display it on the screen.It also puts a lot of pressure on the garbage collector. That is because when the user is scrolling through the list, if a new view is created; the old view (since it is not recycled) is not referenced and becomes a candidate to be picked up by the garbage collector. So what Android does is that it recycles the views and reuses the view that goes out of focus.

Below is a visual representation of this recycle process:

Working-of-an-Android-adapter-Android-adapter

In the above figure, let us assume we are displaying the months in a year in a ListView. To begin with, the months January till May are shown in the screen. When you scroll the view, the month January goes out of the display area of the mobile screen. As soon as the January view goes out of the screen, the Adapter View (ListView in this case) sends the view to something called a recycler.So when you scroll up, the getView () method is called to get the next view (which is June). This method getView() has a parameter called convertview which points to the unused view in the recycler. Through the convertview, the Adapter tries to get hold of the unused view and reuse it to display the new view (which is June in this case).

So, when you are creating a custom Adapter View, you should code your
getView () as mentioned below:

@Override

publicView getView(intitempos, View convertView, ViewGroup parent) {

//Check if the convertview is null, if it is null it probably means that this //is the first time the view has been displayed

if (convertView == null)

{

convertView = View.inflate (context,R.layout.list_content_layout, null);

}

//If it is not null, you can just reuse it from the recycler

TextView txtcontent = (TextView) convertView.findViewById(R.id.textView1);

<code>ImageView imgcontent = (ImageView) </code>convertView<code>.findViewById(R.id.imageView1); </code> <code>&nbsp;</code> <code>Paintings paintingcontent = content [itempos]; </code> txtcontent.setText (paintingcontent.imagetitle); imgcontent.setImageResource(paintingcontent.drawableresid); // return the view for a single item in the listview returnconvertView; 

Android-Adapter

We hope, this Android Adapter tutorial was useful to you! Stay tuned for more tutorials in Android! Happy Learning!

The following resources were used for creating this post: Edureka.co

Got a question for us? Please mention it in the comments section and we will get back to you.

Related Posts:

Get Started with Android Development

Playing Sound with Frame Animation

Android Widgets: Custom Listview

Frame Animation In Android

Comments
38 Comments
  • sri says:

    this is very helpful!!!

    • EdurekaSupport says:

      Hi Sri,
      Thank you for the feedback ! We hope that you will find our blog useful in future as well.
      Keep visiting the Edureka Blog page for latest posts on this link:https://www.edureka.co/blog/

  • Ankit Pandey says:

    A concept clearing post

  • monish monish says:

    thanks for the post its really helps me a lot………………….

    • EdurekaSupport says:

      Hi Monish,
      Thank you for your positive feedback. We hope that you will find our blog useful in future as well. Keep visiting the Edureka Blog page for latest posts on this link:
      https://www.edureka.co/blog/

  • says:

    Hello, nice article! Good job! However, I think there’s a little error. I believe it is the AdapterView which calls the getView() method, which is defined in the Adapter, and not the other way round. Please fix this, or correct me if I am wrong. Cheers! :)

    • EdurekaSupport says:

      Hey Peter, thanks for checking out our blog.
      Adapter interface defines 10 methods that must be implemented by the developer. BaseAdapter is provided as a convenience to Android developers. Rather than creating a class the implements Adapter, SpinnerAdapter or ListAdapter, just extend BaseAdapter and implement the specialized methods for either a ListView or Spinner. What’s more, the BaseAdapter already provides common implementations for many of the interfaces so you don’t have to override these unless there is a specific need.
      At a minimum, you will need to implement four methods. These four methods are called by Android to build your AdapterView and to return the correct information when one of the items in the AdapterView is selected.
      getCount( ): indicates to Android how many items (or rows) are in the data set that will be presented in the AdapterView.
      getItem(int pos): get the data item associated with the item (or row) from the AdapterView passed as a parameter to the method. This method will be used by Android to fetch the appropriate data to build the item/row in the AdapterView.
      getItemId(int pos): This method returns the data set’s id for a item/row position of the AdapterView. Typically, the data set id matches the AdapterView rows so this method just returns the same value.
      getView(int position, View convertView, ViewGroup parent): This method creates the View (which may be a single View component like a TextView or a complex set of widgets in a layout) that displays the data for the specified (by position) item/row in the AdapterView.
      Hope this helps. Cheers!

  • Mahendra Chhimwal says:

    Hi Shivani,
    Nice article . Solve a lot of confusing matter for a newbie for android like me.

    • EdurekaSupport says:

      Glad you found it useful, Mahendra Feel free to go through our other posts as well.

  • Abu Mohammad Rasel says:

    Excellent. i understand it.

    • EdurekaSupport says:

      Thanks Abu!!

  • Chirag says:

    Good Explanation

    • EdurekaSupport says:

      Thanks Chirag. Feel free to go through our other posts.

    • EdurekaSupport says:

      Gald to be of help, Riyaz!!!

  • Epitosoft says:

    I’ve been using listViews for a while now but never truly understood them. This is probably the best explanation I’ve read about android Adapters. I have an internally understanding of it now and I want to thank you a lot!

    • EdurekaSupport says:

      Thanks Epitosoft!! Feel free to browse through our other posts and do comment on them.

  • Sowmya says:

    Very nice. Thank you so much.

    • EdurekaSupport says:

      Hello Sowmya, we are glad you liked this post!

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

Android Adapter Tutorial: What Are Adapters in Android

edureka.co