I'm a real novice in java and especially this object orientating programming, so I would be grateful for any input.
I've made functionality, that changes colour on each selection of rating bar, and because there are few other rating bars that I would like to reuse this functionality on, I've tried to insert the whole code in the method with name for each object and resource id as parameters, but I obviously don't know what I'm doing, as I get error for name variable being already defined in the scope and findViewById being non-static method and being called from a static context.
//rating bar
static void starLight(String name, int resId) {
RatingBar name = (RatingBar) findViewById(resId);
name.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
@Override
public void onRatingChanged(RatingBar ratingBar, float
rating, boolean fromUser) {
int whole = Math.round(rating);
Drawable progress = ratingBar.getProgressDrawable();
if (whole == 1) {
DrawableCompat.setTint(progress,
ResourcesCompat.getColor(getResources(), R.color.colorGreen, null));
}
if (whole == 2) {
DrawableCompat.setTint(progress,
ResourcesCompat.getColor(getResources(), R.color.colorOrange, null));
}
if (whole == 3) {
DrawableCompat.setTint(progress,
ResourcesCompat.getColor(getResources(), R.color.colorRed, null));
}
}
});
}
If you could just shed some light or point me in the right direction I will really appreciate it.
String nameand are trying to declare aRatingBar name- so which one shouldnamerefer to?String nameparameter, so you can just remove it - however you cannot have two different variables with the same name (in the same scope)