I have an assignment to build a simple multi-widget application in Android Studio and I am trying to figure out how to declare multiple variables inside one class for a multi-page photo rating app. I have 4 pages, and 4 photos with a RatingBar
underneath each of them. I would like to have the average of all the ratings displayed on the top of the page. For simplicity I have it being cast to the page title using SetTitle
How can I write this java in such a way that I can access all 4 of my ratings and apply the basic math to them? This is technically beyond what we have been taught so far in this class.
package ca.bcit.comp2052.a00587366.multiplewidgetsapp;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Start of rating bar
RatingBar ratingBar1 = (RatingBar) findViewById(R.id.ratingBar1);
ratingBar1.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
@Override
public void onRatingChanged(RatingBar ratingBar1, float rating, boolean fromUser) {
// Implement your logic here
float total = 0;
total += ratingBar1.getRating();
// total += ratingBar2.getRating();
// total += ratingBar3.getRating();
// total += ratingBar4.getRating();
float average = total / 4;
setTitle("Average: " + average);
}
});
// end of rating bar
Button buttonNext = (Button) findViewById(R.id.nextButton);
buttonNext.setOnClickListener(new
android.view.View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(MainActivity.this,
Main2Activity.class));
}
});
}
}
You should probably have them as R.id.ratingBars[0]
and so forth, but since you don't, assuming it's R.id.ratingBar1
through R.id.ratingBar4
, you can do something like this:
final RatingBar[] ratingBars = {
(RatingBar) findViewById(R.id.ratingBar1),
(RatingBar) findViewById(R.id.ratingBar2),
(RatingBar) findViewById(R.id.ratingBar3),
(RatingBar) findViewById(R.id.ratingBar4)
};
for (final RatingBar ratingBar : ratingBars) {
ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
@Override
public void onRatingChanged(RatingBar bar, float rating, boolean fromUser) {
// Implement your logic here
float total = 0.0f;
for (final RatingBar ratingBar : ratingBars) {
total += ratingBar.getRating();
}
float average = total / 4.0f;
setTitle("Average: " + average);
}
});
}
This also makes it easier to add new RatingBar
s if necessary. You just add them to the original array initialization list at the top and everything is taken care of.
You could try calling them by
Main2Activity.ratingBar2.getRating()
But they should be public, which is not too good.
Elsewhere you can declare them private and the create public getter methods to retrieve them from external classes.
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.