5

評価バーを使用してユーザーのスコアを表示したいと思います。たとえば、ユーザーが 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" />
4

3 に答える 3

7

ここで見つけることができます:

    float d= (float) ((number*5) /100);
    RatingBar rb = (RatingBar) findViewById(R.id.ratingBar1);
    rb.setRating(d);

そしてあなたのレイアウトで:

<RatingBar
    android:id="@+id/ratingBar1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:isIndicator="true"
    android:max="5"
    android:stepSize="0.01" />
于 2013-09-06T16:16:29.427 に答える
0

正確なポイントで評価を表示したい場合は、stepSize を設定する必要があります

rate.setStepSize((float) 0.1);
rate.setRating((float)3.6);

四つ星が半分以上埋まっていることを示します。

于 2015-01-22T09:46:52.087 に答える