Cursor-based large adapter lists scroll faster than the much smaller adapter lists in memory

I have an Android application that has a CursorAdapter -based CursorAdapter (supported by sqlite), as well as a ListView based ListView , created on the fly from JSON, demolished from the server.

The data displayed in both is identical - the image and a pair of TextView s. My cursor-based ListView has 3,000 rows, for a JSON-based ListView about 30. However, scrolling through the list is significantly faster for a cursor-based adapter. For JSON-based lists, all data is retrieved before the rows become visible. Images for both types of lists are uploaded on request.

I have both ListViews configured the same way - both with fastScrollEnabled , scrollingCache and smoothScrollbar set to true.

I am looking for potential customers to do this, to figure out what is happening here, and potentially fix it so that the JSON-based ListView is as fast as the Cursor-based one.

+5
performance android listview
source share
1 answer

I have a similar thing in my application, except that I only have json that supports ListView, and I also have about 30 elements (with constant data changes and animations to reflect the changes). It would be much easier to detect a problem with some of the provided code on your part, but here are a few tricks that you can present for optimization.

  • Reusing convertView, which is passed to you as one of the parameters of the getView method, really speeds up scrolling.
  • Either create your own view of the row (by expanding the layout, or ViewGroup), or use setTag in the row that you return using the getView method. In this tag, you must store an object containing links to the views in this row, so you won’t look for them with findViewById every time a row is requested. An object can be a simple static class, for example

    private static class ViewHolder {

      ImageView image; TextView text; 

    } code>

The first time (when convertView is null and you need to create a new row), you simply create an instance of your ViewHolder and set these parameters to reference the parameters from your newly created row (by calling findViewById) and put this instance in the setTag method of the row . The next time you use convertView, just call getTag, and in the tag you received you will get links to the views in this line, so you no longer need to call findViewById.

Of course, you may already have done all this.

PS I advise you (if you haven’t already done so) to see the Google I / O presentation on ListView. Here is a lot of useful information: http://www.youtube.com/watch?v=wDBM6wVEO70

+3
source share

All Articles