如何使用评级小部件和平均率 - android

时间:2013-10-25 05:59:44

标签: android widget average rates

我正在玩android中的评级小部件。 我知道如何从评级小部件中提取评级数字,但如果我有多个费率,我想平均费率。我怎么能这样做?

这就是我现在所拥有的。

    RatingBar ratingBar = (RatingBar) findViewById(R.id.ratingBar1);
    ratingBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
        @Override
        public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
            TextView rate_lotr_number = (TextView)findViewById(R.id.rate_number1);
            rate_lotr_number.setText(String.valueOf(rating));

        }
    });

    RatingBar ratingBar1 = (RatingBar) findViewById(R.id.ratingBar2);
    ratingBar1.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
        @Override
        public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
            TextView rate_lotr_number = (TextView)findViewById(R.id.rate_number2);
            rate_lotr_number.setText(String.valueOf(rating));

        }
    });

我知道我可以使用一些数组来制作一个循环,以便减少所有重复的文本,但我想尝试在尝试减少冗余之前平均速率。

已编辑:所以我想问一下如何计算许多评级栏的平均值

1 个答案:

答案 0 :(得分:1)

您可以使用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;
这是你想要的吗?请发表评论以避免误解。谢谢。