Android Development (55 Blogs) Become a Certified Professional

Android Adapter Tutorial: What Are Adapters in Android

Last updated on Apr 23,2020 88.7K 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

Upcoming Batches For Android Certification Training Course
Course NameDate
Android Certification Training Course

Class Starts on 4th March,2023

4th March

SAT&SUN (Weekend Batch)
View Details
Android Certification Training Course

Class Starts on 6th May,2023

6th May

SAT&SUN (Weekend Batch)
View Details
Comments
38 Comments
  • Gary says:

    Excellent! Finally, I understood what convertView stood for.

    • EdurekaSupport says:

      Glad this post was useful to you, Gary!!

  • I appreciate you taking the time to write this up. It is a very clear explanation and the graphics only add to the overall understanding. Excellent job!

    • EdurekaSupport says:

      Thanks Daniel!

  • Jorge Casariego says:

    Excellent explanation!

    • EdurekaSupport says:

      Thanks Jorge!!

  • Gaurav says:

    Nice article!!!

  • SANTHOSH says:

    Excellent

  • Donnie Emanuel says:

    धन्यवाद

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