简体   繁体   English

评级栏未显示正确的评级。 仅显示填充的星星

[英]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 . 我正在使用一个在RelativeLayout设置的评级栏以及一个ImageView和两个TextViews The RelativeLayout is placed in a row in my ListView. RelativeLayout放在我的ListView中的一行中。

The ratingbar recieves its rating from the item, that's selected in the ListView, but doesn't display the correct "star values". 评级栏从项目中获取其评级,该项目在ListView中选择,但不显示正确的“星形值”。

<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. 例如:setRating(3.5f)将导致4颗满星。

To make things more complicated: getRating(); 使事情变得更复杂:getRating(); would return the correct float-Value (in this case 3.5) 将返回正确的float值(在本例中为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. alc将显示在此代码的第一行中收到的正确值。 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. 为了清楚起见:我的评级栏DOES接受浮点值,因为getRating()方法返回我使用setRating()方法设置的正确浮点值。 The problem is, the ratingbars stars only show completely filled stars, although I set the stepSize to 0.1f. 问题是,虽然我将stepSize设置为0.1f,但ratingbars星只显示完全填满的星星。

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  -->

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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