-1

I'm playing around with the rating widget in android. I know how to pull out the rating number from the rating widget but if I have more than one rates and I want to average the rates. How can I do that?

This is what I have at the moment.

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

        }
    });

I know I could probably use a look a some arrays to make a loop so to reduce all those repeating texts but I want to try how to average the rates before trying to reduce the redundancy.

EDITED: so what I want to ask how can I calculate the average of many ratingbars

WXR
  • 481
  • 1
  • 7
  • 18
  • Could you elaborate more about the problem? Do you want to _only_ calculate the average rating from many `RatingBar`? Or, are there something more? – Andrew T. Oct 25 '13 at 06:11
  • oops YES I do want to calculate the average rating from many rating bar. Sorry for my bad english. The redundancy part I want to try that myself later first before asking questions. – WXR Oct 25 '13 at 06:13

1 Answers1

1

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.

Andrew T.
  • 4,701
  • 8
  • 43
  • 62
  • I believe this is what I want. I will try it in few minutes. – WXR Oct 25 '13 at 06:26
  • from what you said I believe that's what I want but somehow when I implement it I get error though. I implement your code inside a button `setOnClickListener` with a `textView` to show the average but I have red line saying `RatingBar.getRating()` is an error – WXR Oct 25 '13 at 06:36
  • If possible, could you post your logcat here? Where did you put this code? Make sure that `ratingBar` and `ratingBar1` can be accessed (or it will give `NullPointerException`). – Andrew T. Oct 25 '13 at 06:40
  • let me post the codes first I will edit your reply and put in your reply is that ok? and logcat...let me find it...I'm totally new with this so I'm playing around and watching the tutorial in android developer. – WXR Oct 25 '13 at 06:43
  • ah I realized I put RatingBar instead of ratingBar..but how I can bring the `float average` inside the Button listener so when I click the button the `average` will show? – WXR Oct 25 '13 at 06:55
  • While this is not efficient, this is a quick solution: paste the above code snippet inside your `onClick()`, including the declaration of `ratingBar` and `ratingBar1`, but: use `YourActivityName.this.findViewById(R.id.ratingBar1);` instead. – Andrew T. Oct 25 '13 at 07:15
  • omg thanks. Why didn't I think of that. But the solution works. – WXR Oct 25 '13 at 07:23
  • how is it not efficient though? – WXR Oct 25 '13 at 07:24
  • Because `RatingBar ratingBar = (RatingBar) findViewById(R.id.ratingBar1);` will be called every time the user clicks the button. Maybe it won't impact the performance at all, but I prefer to declare the widgets (in this case, `RatingBar`) as class variables, then call `ratingBar = (RatingBar) findViewById(R.id.ratingBar1);` in `onCreate()`, so it will be called once and you still have access to the rating bars. :) – Andrew T. Oct 25 '13 at 07:28
  • ah..by doing it that way do I have to make lots changes? – WXR Oct 25 '13 at 19:48
  • It depends on your current code: Move the widget declarations to be class variables, initialize them in `onCreate()` and remove from the rest of the code (unless you need to). Then your widgets can be accessed from anywhere in that file. – Andrew T. Oct 26 '13 at 04:47