Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android convertView, to use it or not?

In the article Multithreading For Performance from Android Developer Blog, convertView is used in Adapter.getItem() to display a list of ImageView, which are downloaded through HttpRequest.Yet, I also see some Android tutorials that don't use the convertView, and just inflate a new view in Adapter.getItem().

I'm wondering what's the advantage of using convertView? How does it recycle the used view? Where are the used view cached?

I ask this question because I didn't use convertView in my project and I want to know the cost for not using it. Below is my implementation of a listView of images.

Instead of using convertView, I inflate a new view in Adapter.getItem(). Besides, I create a wrapper class to hold the staff in each item of listView. Once the image is downloaded, it will be stored as bitmap in the wrapper class for each list item(The image is small). In this way, I can avoid the duplicate downloading of the same image. And this also avoid the race condition issues talked in Multithreading For Performance. But I'm still a little worried, is there any issues that not good by using my method?


FYI: the article recommended by @Carl Anderson gives details about how convertView works in adapter. The Google IO by Romain Guy that is suggested in this article is another good reference.

In a word, using convertView is both space and time optimized. Besides, I've abandoned my own imageDownloader and use the Picasso that is recommended by @CommonsWare. It works like a charm.

like image 845
user2060386 Avatar asked Apr 29 '14 21:04

user2060386


People also ask

What is the significance of a convertView?

The Adapter uses the convertView as a way of recycling old View objects that are no longer being used. In this way, the ListView can send the Adapter old, "recycled" view objects that are no longer being displayed instead of instantiating an entirely new object each time the Adapter wants to display a new list item.

What is Base adapter in Android?

BaseAdapter, as it's name implies, is the base class for so many concrete adapter implementations on Android. It is abstract and therefore, cannot be directly instantiated. If your data source is an ArrayList or array, we can also use the ArrayAdapter construct as an alternative.

How to use ArrayAdapter Android?

Go to app > res > layout > right-click > New > Layout Resource File and create a new layout file and name this file as item_view. xml and make the root element as a LinearLayout. This will contain a TextView that is used to display the array objects as output.

What is ListView and Base adapter why we use Base adapter?

BaseAdapter is a common base class of a general implementation of an Adapter that can be used in ListView, GridView, Spinner etc. Whenever you need a customized list in a ListView or customized grids in a GridView you create your own adapter and extend base adapter in that.


2 Answers

The problem with not re-using the convertView is that, as you said, you are creating and inflating a new View each and every time the user scrolls a row into view. Creating and inflating a view is a huge amount of time compared to reusing existing views, and you are certainly paying a performance hit for it.

My app does something similar - it uses ImageViews inside of rows in a ListView, and those ImageViews are populated by images that are downloaded in the background. As part of my investigation into a threading bug, I turned off reuse of Views, and the performance was absolutely terrible when I did that. ListView code is optimized for view reuse, and if you don't hold up to that, your framerate and usability will suffer.

Reading this link also helped me understand a lot better how ListViews work: http://lucasr.org/2012/04/05/performance-tips-for-androids-listview/

like image 50
Carl Anderson Avatar answered Sep 27 '22 01:09

Carl Anderson


I also see some Android tutorials that don't use the convertView, and just inflate a new view in Adapter.getItem().

That is not a good sign.

I'm wondering what's the advantage of using convertView?

It saves CPU time and generates less garbage in the heap.

How does it recycle the used view?

It is the used view.

Where are the used view cached?

In AdapterView.

I ask this question because I didn't use convertView in my project and I want to know the cost for not using it.

Well, depending on how you wrote your adapter, you might be getting row recycling "for free" from your superclass.

Below is my implementation of a listView of images.

There is no code in your question.

Instead of using convertView, I inflate a new view in Adapter.getItem().

Only do that if convertView is null. Otherwise, use convertView. This takes one if/else construct.

Once the image is downloaded, it will be stored as bitmap in the wrapper class for each list item(The image is small). In this way, I can avoid the duplicate downloading of the same image.

I do not quite understand where/how you are downloading the image. There are many, many libraries that do this for you, and using one is usually a better idea than is rolling your own code. I like Picasso and Ion for this, but there are others.

But I'm still a little worried, is there any issues that not good by using my method?

There may be more than what I have listed, but without seeing any code, it is difficult to guess what may be not good.

like image 24
CommonsWare Avatar answered Sep 26 '22 01:09

CommonsWare