In my Android Application Development for Dummies book, I cover working with list views. The example I provided works excellent and you can follow along in the book to implement the given example. This post is about how you can improve the performance of the ListView and its items.
When rendering a list view in the book, I override the getView method on the ListActivity. At that point I inflate the view from the resources. The code snippet looks like this:
view = mInflater.inflate(R.layout.row, parent, false);
This happens each time a view is created on the list view. Unforatunately, this is quite slow on lists with tons of items (say, for example – 10,000 items).The reason this is slow is because each time the list view has to go to a new item, it must be inflated from XML manually, parsed, and then objects have to be instantiated and rendered. Doing this 10,000 times is very very slow.
To improve the speed in which the list view renders the items the list view only builds the views which are on the screen. That means if you have 10,000 items you want to put in the listview, and only 10 are on the screen at once, then only 10 views are being shown. As the user flings the screen and list items are moved out of view the ListView will take that view and recycle it, giving it back to getView(…) as the “convertView” parameter. This view is already hydrated and inflated. Therefore, you can improve the performance of your list view by simply changing the above code to this:
if(convertView == null) { view = mInflater.inflate(R.layout.row, parent, false); } else { view = convertView; } // Or you can just always use "convertView" and if its null, inflate it the first time. If thats the case you'd only need // to use the 'if' part of the statement above
You will now be using a recycled view and the android view system will not have to work overtime inflating views over and over and over.
To really polish up your ListActivity skills, watch the “The world of ListView” Google IO Video by Romain Guy and Adam Powell
Eric says
I see that you can buy Kindle and Nook versions of the book, but how about a PDF version?
I went to the B&N near the Ridgedale mall and didn’t see any copies of your book.
I’d prefer to get a PDF version, because I do most of my reading on the computer these days (plus it’s a lot easier recycling a PDF than a dead tree book).
Thanks!
Donn Felker says
I’m not sure if any PDF versions will (or are) available. Right now I only
know of Kindle and Nook.
Sorry!
Donn
Eric says
I just remembered that there is a PC version of Kindle, so that should work!
http://www.amazon.com/gp/feature.html?ie=UTF8&docId=1000579091
I think you can also purchase and read the book through the new Google ‘bookstore’?
http://books.google.com/ebooks?id=1C3yNgqZnUkC&dq=donn%20felker&as_brr=5&source=webstore_bookcard