Android Adapter Tutorial: What Are Adapters in Android

Recommended by 202 users

Mar 19, 2013
Android Adapter Tutorial: What Are Adapters in Android
Add to Bookmark Email this Post 72.9K    37

Adapters in Android are a bridge between the Adapter View (e.g. ListView) and the underlying data for that view. Imagine 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> </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

How to Create an alert dialog in Android

Notification In Android

Playing Sound with Frame Animation

Android Widgets: Custom Listview

Frame Animation In Android

Share on
Comments
37 Comments
  • Shikha Tewari

    hey can you plz tell me exact meaning of inflating view or layout. how is it related to adapter ? plx reply

    • EdurekaSupport

      Hey Shikha, thanks for checking out our blog. Here’s the answer to your query.
      Inflating means to read the XML file that describes a layout (or GUI element) and to create the actual objects that correspond to it, and thus make the object visible within an Android app. Layout-Inflater class is used to instantiate layout XML file into its corresponding View objects.
      In other words, it takes as input an XML file and builds the View objects from it.
      Hope this helps. Cheers!

  • Honey Patel

    Nice I clearly understand Adapter

  • Shreyans

    The article has clarified my doubts about Adapters. Got the concept easily. Thanks!

    • EdurekaSupport

      Thanks Shreyans! Happy we could be of help. We urge you to keep checking the Edureka blog page for many more such articles.

24 X 7 Customer Support X

  • us flag 1-800-275-9730 (Toll Free)
  • india flag +91 88808 62004