简体   繁体   中英

Ratingbar not showing correct rating. Only filled stars are shown

I'm working on a ratingbar which is set in an RelativeLayout together with an ImageView and two TextViews . The RelativeLayout is placed in a row in my ListView.

The ratingbar recieves its rating from the item, that's selected in the ListView, but doesn't display the correct "star values".

<RatingBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/item_rating"
        android:layout_toRightOf="@+id/item_icon"
        android:layout_below="@+id/item_Name"
        android:numStars="5"
        android:rating="3.5"
        android:stepSize="0.1"/>

Eg: setRating(3.5f) would result in 4 filled stars.

To make things more complicated: getRating(); would return the correct float-Value (in this case 3.5)

float rating_value = currentCocktail.getRating();
        RatingBar rating = (RatingBar)itemView.findViewById(R.id.item_rating);
        rating.setIsIndicator(true);
        rating.setNumStars(5);
        rating.setMax(5);
        rating.setStepSize(0.1f);
        rating.setRating(rating_value);

        alc.setText(""+rating.getRating());

The above code is used to set the rating. alc will show the correct value which was received in the first line of this code. Nevertheless, the stars aren't displayed correctly.

To make things clear: My rating bar DOES accept float values as the getRating() method returns the correct float value I set with the setRating() method. The problem is, the ratingbars stars only show completely filled stars, although I set the stepSize to 0.1f.

try this code:

rate_bar.setRating(Float.parseFloat("2.0"));

or use directly float this:

rate_bar.setRating(0.0f);

If you want to set default rating value you can use

<RatingBar
        android:id="@+id/ratingBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:numStars="4"               <-- total number of stars -->
        android:stepSize="1.0"
        android:rating="2.0" />             <-- default value  -->

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM