Android Certification Training Course
- 64k Enrolled Learners
- Weekend
- Live Class
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 Certification. Imagine what a world it would have been without Android Adapters!
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.
Quite cumbersome job! Isn’t it?
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!
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.
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;
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
Course Name | Date | |
---|---|---|
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 |
edureka.co
Excellent! Finally, I understood what convertView stood for.
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!
Thanks Daniel!
Excellent explanation!
Thanks Jorge!!
like it
nice
Nice article!!!
Excellent
nice one.
gud jobbbbb………
धन्यवाद