简体   繁体   中英

How to calculate and display the average rating in a rating bar?

I'm a beginner programmer and I am working on an app that displays items. Users can rate an item using a rating bar and also write a comment. The rating bar has a scale too.

How can I calculate the average rating of the item and display it in a new rating bar? Any ideas?

RatingBar mRatingBar = (RatingBar) findViewById(R.id.ratingBar);
    EditText mFeedback = (EditText) findViewById(R.id.etFeedback);
    Button mSendFeedback = (Button) findViewById(R.id.btnSubmit);
    TextView mRatingScale = (TextView) findViewById(R.id.tvRatingScale);
mRatingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
            @Override
            public void onRatingChanged(RatingBar ratingBar, float v, boolean b) {
                mRatingScale.setText(String.valueOf(v));
                switch ((int) ratingBar.getRating()) {
                    case 1:
                        mRatingScale.setText("Pretty bad");
                        break;
                    case 2:
                        mRatingScale.setText("Just okay");
                        break;
                    case 3:
                        mRatingScale.setText("Good");
                        break;
                    case 4:
                        mRatingScale.setText("Very good");
                        break;
                    case 5:
                        mRatingScale.setText("Amazing!");
                        break;
                    default:
                        mRatingScale.setText("");
                }
            }
        });
        mSendFeedback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mRatingBar.getRating() == 0.0 ) {
                    Toast.makeText(EventDemo.this, "Please enter a rating", Toast.LENGTH_LONG).show();
                } else {
                    mFeedback.setText("");
                    mRatingBar.setRating(0);
                    Toast.makeText(EventDemo.this, "Thanks for the rating", Toast.LENGTH_SHORT).show();
                }
            }

You can get each rating value using RatingBar.getRating(), sum all the values and divide it by the number of rating bars.

    RatingBar ratingBar = (RatingBar) findViewById(R.id.ratingBar1);
RatingBar ratingBar1 = (RatingBar) findViewById(R.id.ratingBar2);

float total = 0;
total += ratingBar.getRating();
total += ratingBar1.getRating();
float average = total / 2;

Is this what you want? Please comment to avoid misunderstanding. Thank you.

public class MainActivity extends AppCompatActivity { private RatingBar rBar; private TextView tView; private Button btn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); rBar = (RatingBar) findViewById(R.id.ratingBar1); tView = (TextView) findViewById(R.id.textview1); btn = (Button)findViewById(R.id.btnGet); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int noofstars = rBar.getNumStars(); float getrating = rBar.getRating(); tView.setText("Rating: "+getrating+"/"+noofstars); } }); } }

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