評価バーを使用してユーザーのスコアを表示したいと思います。たとえば、ユーザーが 30 問中 7 問正解した場合、テキスト ビュー (tv_percentage) は 23.33% で正しく表示されますが、評価星は 5 つ星のうち 1.17 つ星を表示するはずですが、スコアが何であれ、常に5つ星を表示しています。
私は次のことに対して何が間違っていましたか?
コード:
SharedPreferences score = this.getSharedPreferences("MyApp", 0);
int userscore = score.getInt("score", 0);
int userQnumber = score.getInt("number_of_question_to_selected", 9999);
double scoring100 = Double.valueOf(userscore) / Double.valueOf(userQnumber) *100;
String stripped2 = Double.valueOf(scoring100).toString();
DecimalFormat myFormatter1 = new DecimalFormat("###,###,###.##");
stripped2 = myFormatter1.format(Double.valueOf(stripped2));
tv_percentage.setText(""+stripped2+"%");
ratingBar1.setRating(Float.parseFloat(stripped2));
編集されたコード:
float d= (float) (scoring100 /100 * 5);
String S = Double.valueOf(d).toString();
tv2_90.setText(""+S); // for testing only, tv2_90 showing proper value
ratingBar1.setStepSize((d));
ratingBar1.setRating(Float.parseFloat(stripped2));
レイアウト:
<RatingBar
android:id="@+id/ratingBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:isIndicator="true"
android:max="100"
android:numStars="5"
android:stepSize="0.1" />