如何计算和显示评分栏中的平均评分?

时间:2020-04-14 16:57:44

标签: java android rating ratingbar

我是一名初学者,正在开发一个显示项目的应用程序。用户可以使用评级栏对项目进行评级,也可以发表评论。评分栏也有刻度。

如何计算商品的平均评分并将其显示在新的评分栏中?有什么想法吗?

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();
                }
            }

2 个答案:

答案 0 :(得分:0)

您可以使用RatingBar.getRating()获取每个评级值,将所有值求和,然后除以评级栏数。

    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;

这是您想要的吗?请发表评论以避免误会。谢谢。

答案 1 :(得分:0)

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);
            }
        });
    }
}