Fancy ListViews, Part Two PDF Print E-mail

In our last episode, we saw how to create Android ListViews that contain more than just a simple list of strings. In particular, we saw the ultimate form of customization: subclassing an Adapter class, overriding getView(), and returning our own View for each row, perhaps based on our own layout XML inflated via ViewInflate.

 

As Romain Guy pointed out, though, I left out a piece of the puzzle.

Phones aren’t the speediest things out there. In Android, creating widgets (Views) is a comparatively expensive operation, not to mention the garbage collection of getting rid of them once you’re done. Hence, you don’t want to create any more widgets than you have to.

Now, let’s imagine we have a ListView that, on-screen, can show 7 entries, but the list itself has 50 entries, perhaps from a database or some parsed Internet content. Creating widgets for 7 entries is much less expensive than creating widgets for 50 entries. Yet, our getView() implementation from the last post will create widgets whenever it is asked. If you add Log statements to see what happens, you will see that, if you scroll through the whole list, you will be called 50 times.

Urk!

Android has a way to help you improve your getView() performance; as Romain Guy indicated, it’s via the contentView parameter passed into getView().

Sometimes, convertView will be null. In those cases, you have to create a new row View from scratch (e.g., via inflation), just as we did before.

However, if convertView is not null, then it is actually one of your previously-created Views! This will happen primarily when the user scrolls the ListView — as new rows appear, Android will attempt to recycle the views of the rows that scrolled off the other end of the list, to save you having to rebuild them from scratch.

Assuming that each of your rows has the same basic structure, you can use findViewById() to get at the individual widgets that make up your row and change their contents, then return contentView from getView(), rather than create a whole new row.

For example, here is the getView() implementation from last time, now optimized via contentView:

 
  1. public View getView(int position, View convertView, ViewGroup parent) { 
  2.     View row=convertView; 
  3.  
  4.     if (row==null) { 
  5.         ViewInflate inflater=context.getViewInflate(); 
  6.  
  7.         row=inflater.inflate(R.layout.row, null, null); 
  8.     } 
  9.  
  10.     TextView label=(TextView)row.findViewById(R.id.label); 
  11.  
  12.     label.setText(items[position]); 
  13.  
  14.     if (items[position].length()>4) { 
  15.         ImageView icon=(ImageView)row.findViewById(R.id.icon); 
  16.  
  17.         icon.setImageResource(R.drawable.delete); 
  18.     }    
  19.  
  20.     return(row); 

Here, we check to see if the contentView is null and, if so, we then inflate our row — but if it is not-null, we just reuse it. The work to fill in the contents (icon image, text) is the same in either case. The advantage is that we avoid the potentially-expensive inflation step.

This approach will not work in every case, though. For example, the TourIt sample application in my book uses a ListView to show the cue sheet for a bicycle tour. Think of a cue sheet as Google Map directions, but hand-written to deal with idiosyncracies of cycling (e.g., avoiding nasty intersections). Each row in the cue sheet represents one step in the directions (e.g., ride 0.5 miles and turn left onto Mosser Blvd.).

However, some steps in the cue sheet have additional notes (e.g., “high traffic area!”). So all this can be readable, sometimes TourIt uses a single line of text (plus icons) in the row, and sometimes two lines of text, as shown below:

In this case, recycling existing rows becomes tricky, as the layouts may significantly differ. For example, if the row we need to create a View for requires two lines of text, we cannot just use a View with one line of text as-is. We either need to tinker with the innards of that View, or ignore it and inflate a new View.

Of course, there are ways to deal with this, such as making the second line of text visible or invisible depending on whether it is needed. And, on a phone, every millisecond of CPU time is precious, possibly for the user experience, but always for battery life — more CPU utilization means a more quickly-drained battery.

That being said, particularly if you are a rookie to Android, I recommend focusing on getting the functionality right first, then looking to optimize performance on a second pass through your code, rather than get lost in a sea of Views trying to tackle it all in one shot.

 

Source: http://androidguys.com/2008/07/17/fancy-listviews-part-two/

Comments (13)Add Comment
tiffany on sale
written by tiffany on sale , May 22, 2010
That being said, particularly if you are a rookie to Android, I recommend focusing on getting the functionality right first, then looking to optimize performance on a second pass through your code, rather than get lost in a sea of Views trying to tackle it all in one shot.
report abuse
vote down
vote up
Votes: +0
birkenstock shoes
written by birkenstock , May 29, 2010
When you are on a holiday in some seaside resort where you will hike and walk on cliffs, Birckenstock is one of the brands to which you turn your attention to find some slippers or flip-flops that make a holiday in comfort and without pain in the feet after a romantic walk on the rocks.Birkenstocks is the well-known German footwear brand: Birkenstock shoes, Birkenstock sandals and Birkenstock clogs.Birkenstock Gizeh is a flattering flip-flop style with a leather upper that will look great with shorts, jeans or dresses, at the same time, it has an irresistible air between the tourist and hiking, and you can be sure that the evening will have no pain, given the high quality these shoes.
report abuse
vote down
vote up
Votes: +0
...
written by supra shoes , May 31, 2010
The are popular with both recreation and Supra Shoes
Supra Footwear sportswear are simply matchless in terms of style, comfort and longevity, Now! Free Shipping! NO Tax!
report abuse
vote down
vote up
Votes: +0
air max 2009
written by air max 2009 , May 31, 2010
are you looking for a air max 2009?
here are not only offer the nike free shoes ,but aslo for thenew lebron VII shoes
report abuse
vote down
vote up
Votes: +0
...
written by mbt , June 18, 2010
While in this case we added new tabs based on button clicks
report abuse
vote down
vote up
Votes: +0
...
written by authentic jordans , June 18, 2010
thanks for your post!!!!!!!!!
report abuse
vote down
vote up
Votes: +0
but the list itself has...
written by coach bags , June 19, 2010
but the list itself has 50 entries, perhaps from a database or some parsed Internet content. Creating widgets for 7 entries is much less expensive than creating widgets for 50 entries
report abuse
vote down
vote up
Votes: +0
Cheap San Francisco 49ers Jerseys
written by sexy bikinis , June 24, 2010
It is not the NFL Jerseys critic who counts,not the man who points out how the strong man stumbles,the doer of deeds could have sexy bikinis sale done them better. The credit belongs to the man who is actually in the arens,whose face is marred by dust and sweat and blood.
report abuse
vote down
vote up
Votes: +0
mbt
written by mbt shoes , June 29, 2010
Creating widgets for 7 entries is much less expensive than creating widgets for 50 entries .


vibram kso
report abuse
vote down
vote up
Votes: +0
...
written by critique of research article , July 07, 2010
Thank you so mush for such an interesting information which I'll use in my Resume writing, it can be helpful for you if you don't want to buy resume
report abuse
vote down
vote up
Votes: +0
...
written by buy resume , July 07, 2010
thnks foe cool articles, author
report abuse
vote down
vote up
Votes: +0
Cheap GHDs
written by Cheap GHDs , July 17, 2010
Cheap ghds is is provided purely as an information based service.
Cheap GHDs
We provide information, specification, and price comparisons of ghd products, both promoting them and helping you find the best online prices from reputable retailers bringing you the cheapest ghds online.
GHD Straighteners
We try to ensure that information on this website is as accurate as possible,
GHD Colour Collection
however we accept no responsibility for any errors on this website whatsoever. This website is updated throughout the day,
GHD IV Styler
every day to ensure that the information you see is as accurate as possible.
GHD Precious Gift Set
This site is in no way owned by or affiliated with ghd and is an unofficial ghd price comparison website.
GHD IV Styler Dark
All images, logos and trademarks belong to their respective owners.
GHD Radiance Benefit Set
By using this website you are agreeing to our Privacy policy and Terms & Conditions.
GHD Hair Straightener
GHD on sale
GHD IV Salon Styler
GHD IV Mini Styler
GHD IV Gold Styler
GHD Kiss IV Styler
GHD Pure IV Styler
GHD Purple IV Styler
GHD Black IV Styler
GHD Benefit Styler
GHD Rare Styler
GHD Pink Styler
report abuse
vote down
vote up
Votes: +0
timberland mens roll top
written by timberland mens roll top , July 27, 2010
Here was someone immersed in a search for truth and beauty. Words had been cheap timberland boots treasured, words that were beautiful. And I felt as if the words timberland waterproof boots somehow delighted in being discovered, for they were obviously very generous to the as yet anonymous writer of the notes. And now this person was in turn learning the secret of sharing them. Beauty so shines when given away. smilies/smiley.gif
report abuse
vote down
vote up
Votes: +0

Write comment
quote
bold
italicize
underline
strike
url
image
quote
quote
smile
wink
laugh
grin
angry
sad
shocked
cool
tongue
kiss
cry
smaller | bigger

security code
Write the displayed characters


busy
 

 

This domain is for sale! Please contact me via contact page!