Fancy ListViews Redux: 0.9 SDK and RatingBar PDF Print E-mail

You may remember way back when (e.g., July 2008) when Building ‘Droids featured a six-post series on creating fancy ListView implementations, culminating in a CheckListView widget that could be used as a drop-in replacement for ListView.

 

They’re ba-ack!

Specifically, today, let’s take a look at the 0.9 SDK’s impact on the Fancy ListView series, and update one of the examples to take advantage of a new widget: the RatingBar.

In many respects, the code and techniques introduced in the Fancy ListViews series are still valid and relevant in the world of the 0.9 SDK. Perhaps the biggest change is that ViewInflate became LayoutInflater, requiring some search-and-replace edits of your older M5 source code. But the concepts of supplying custom views, of recycling views using the ViewHolder/ViewWrapper pattern, and the like are still very useful, even after the 0.9 SDK.

Of course, as promised, the 0.9 SDK has its own take on having checkable ListView widgets; this will be covered in a future blog post.

For now, though, let’s revisit the last sample from the Fancy ListViews series and update it to eschew the checkbox, switching to the new RatingBar.

RatingBar is a widget designed to users to provide a rating to something, such as rating a music track or a blog post or an Android development book up on Amazon.com (*cough*). A rating is a float, from 0 to a specified maximum (android:numStars). You can specify what the granularity of the rating is (android:stepSize) and whether it is merely an indicator (android:isIndicator) or if it is a user-input element to allow users to set the rating by sliding their finger across the stars.

As with just about any moderately-sized widget, the RatingBar can be used as part of a row in a ListView. In fact, replacing the CheckBox in CheckListView with a RatingBar is fairly straight-forward:

 
  1. public class RateableWrapper extends AdapterWrapper { 
  2. Context ctxt=null
  3. float[] rates=null
  4.  
  5. public RateableWrapper(Context ctxt, ListAdapter delegate) { 
  6. super(delegate); 
  7.  
  8. this.ctxt=ctxt; 
  9. this.rates=new float[delegate.getCount()]; 
  10.  
  11. for (int i=0;i<delegate.getCount();i++) { 
  12. this.rates[i]=2.0f; 
  13.  
  14. public View getView(int position, View convertView, ViewGroup parent) { 
  15. ViewWrapper wrap=null
  16. View row=convertView; 
  17.  
  18. if (convertView==null) { 
  19. LinearLayout layout=new LinearLayout(ctxt); 
  20. RatingBar rate=new RatingBar(ctxt); 
  21.  
  22. rate.setNumStars(3); 
  23. rate.setStepSize(1.0f); 
  24.  
  25. View guts=delegate.getView(position, null, parent); 
  26.  
  27. layout.setOrientation(LinearLayout.HORIZONTAL); 
  28.  
  29. rate.setLayoutParams(new LinearLayout.LayoutParams( 
  30. LinearLayout.LayoutParams.WRAP_CONTENT, 
  31. LinearLayout.LayoutParams.FILL_PARENT)); 
  32. guts.setLayoutParams(new LinearLayout.LayoutParams( 
  33. LinearLayout.LayoutParams.FILL_PARENT, 
  34. LinearLayout.LayoutParams.FILL_PARENT)); 
  35.  
  36. RatingBar.OnRatingBarChangeListener l=new RatingBar.OnRatingBarChangeListener() { 
  37. public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromTouch)  { 
  38. rates[(Integer)ratingBar.getTag()]=rating; 
  39. }; 
  40.  
  41. rate.setOnRatingBarChangeListener(l); 
  42.  
  43. layout.addView(rate); 
  44. layout.addView(guts); 
  45.  
  46. wrap=new ViewWrapper(layout); 
  47. wrap.setGuts(guts); 
  48. layout.setTag(wrap); 
  49.  
  50. rate.setTag(new Integer(position)); 
  51. rate.setRating(rates[position]); 
  52.  
  53. row=layout; 
  54. else
  55. wrap=(ViewWrapper)convertView.getTag(); 
  56. wrap.setGuts(delegate.getView(position, wrap.getGuts(), parent)); 
  57. wrap.getRatingBar().setTag(new Integer(position)); 
  58. wrap.getRatingBar().setRating(rates[position]); 
  59.  
  60. return(row); 
  • Create and configure the RatingBar when it is lazy-instantiated in getView()
  • Hook in a RatingBar.OnRatingBarChangeListener to update a float[] of ratings, rather than a boolean[] of checkbox states
  • Update the rating in the RatingBar when it is recycled
  • Rename all the classes to something more logical (e.g., CheckableWrapper becomes RateableWrapper)

Unfortunately, the stock style for RatingBar is a bit big for lists:

A sample ListView using a RatingBar in each row

You can use a ratingBarStyleSmall style to shrink the size, but then the RatingBar becomes read-only, meaning you would need some other means to let people specify the rating itself.

 

Source: http://androidguys.com/2008/08/26/fancy-listviews-redux-09-sdk-and-ratingbar/

Comments (11)Add Comment
replica tiffany
written by replica tiffany , May 22, 2010
You can use a ratingBarStyleSmall style to shrink the size, but then the RatingBar becomes read-only, meaning you would need some other means to let people specify the rating itself.
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 jordan shoes , May 31, 2010
would a little more money make us a little happier? Many of us smirk and nod. There is, we believe, some connection between fiscal fitness and feeling fantasti air jordan shoes
. Most of us would say that, yes, we would like to DC. michael jordan shoes
Three in four American collegians now consider it.
report abuse
vote down
vote up
Votes: +0
air max 2009
written by air max 2009 , May 31, 2010
we do the sports all the time ,we need to work , to study ,to do sports,and so on , so we need a pair of the comfortable shoes is neccessary in our daily life,basketballshoesdiscount.com provide you the widely range of the nike shoes,for example, air max 2009, nike lebron james shoes,nike free shoes,kobe basketballshoes ,womens free running shoes and so on for all the women and men .and we updated the shoes everyday ,weclome to choose nike shoes for the coming summer .moreover, A healthy body needs a good pair of the shoe, meaning you have to wear good shoes for you. we offer all the kinds of the goods shoes ,just like the womens nike free 5.0 shoes, mens nike free 7.0 shoes, womens nike air max 2009 shoes,mens nike air max 2010 shoes,etc. People can not lack sports,can not lack of a pair of good shoes.and now the nike lebron james VI ,nike lebron james VII , womens nike free 7.0 shoes, mens air max 2009 shoes,mens nike air max 95 shoes,womens nike air max 90 shoes,are hot sale now . to choose the nike shoes from the basketballshoesdiscount to enjoy the superb service with high quality.
report abuse
vote down
vote up
Votes: +0
...
written by mbt , June 18, 2010
contests administered by SUN, ACM, and IBM. He also had co-authored U.S.
report abuse
vote down
vote up
Votes: +0
sf
written by louis vuitton , June 19, 2010
contests administered by SUN, ACM, and IBM. He also had co-authored U.S.
report abuse
vote down
vote up
Votes: +0
A rating is a float
written by coach factory outlet , June 19, 2010
A rating is a float, from 0 to a specified maximum (android:numStars). You can specify what the granularity of the rating is (android:stepSize) and whether it is merely an indicator
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
You can specify what the granularity of the rating is (android:stepSize) and whether it is merely an indicator .
ugg boots

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
Famous Hats
written by Famous Hats , July 28, 2010
The dissertation chapters should be perfectly composed by distinguished thesis service, when people want to present a writing talent. Thus, this is manifestly that you understand the right way to finish a superb issue referring to this topic. Thank you very much for distributing this.
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!