0

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.

3
  • 1
    You have a String name and are trying to declare a RatingBar name - so which one should name refer to? Commented Apr 24, 2017 at 16:51
  • @UnholySheep correct me cause I'm probably wrong. If I have few rating bars in my layout file, and want to add that custom listener to each one of them, don't I have to create and give different name to each one of those objects that I'm linking through findViewById? And set the listener to each one of them? So i thought if I pass different name and correct id as parameters I will be able to initialise each one of them like that. Commented Apr 24, 2017 at 19:52
  • As Michael points out in his answer, you are not using the String name parameter, so you can just remove it - however you cannot have two different variables with the same name (in the same scope) Commented Apr 24, 2017 at 19:54

2 Answers 2

1

Why don't you pass in a particular RatingBar instead of the Resource id? Then you can leave the method signature as static. And as others have pointed out, there is no need for the String nameparameter. Like this:

static int green = ResourcesCompat.getColor(getResources(), R.color.colorGreen, null));


static void starLight(RatingBar ratingBar) {

 Drawable progress = ratingBar.getProgressDrawable();
        if (whole == 1) {
            DrawableCompat.setTint(progress, green);
...}
Sign up to request clarification or add additional context in comments.

3 Comments

that nearly worked. getResources still throws an error.
Ok, define your colors as ints outside of the method (class level variables) and just use those variables in the method.
Thank you @mondakuwar that worked. I've passed those colors as final int parameters of the method and it works now. Something clicked in my head, but it still feels really overwhelming. You definitely did help me with this, and I'm really grateful for this.
1

It seems that you just need to change the function signature and remove the String parameter:

static void starLight(/*String name,*/ int resId) {

I don't know what you intended to use it for but it seems like you don't actually use it for anything.

The reason you get a compiler error is because you have two variables called 'name': one as a parameter, one as a local variable inside the method body.


As a side note, your current version duplicates a lot of code. You can refactor this:

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));
}
// etc.

to something like

Drawable progress = ratingBar.getProgressDrawable();
int colour;
switch (whole)
{
    case 1:
        colour = R.color.colorGreen;
        break;
    case 2:
        colour = R.color.colorOrange;
        break;
    case 3:
        colour = R.color.colorRed;
        break;
    default:
        colour = //something. Or throw an exception maybe?
}
DrawableCompat.setTint(progress, ResourcesCompat.getColor(getResources(), colour, null));

Interestingly, this does end up being longer than what you've got now, but I'd argue it's much easier to see what's going on because the information is sort of less dense. Also, if you change the way you set the tint, now you only need to do it in one place.

2 Comments

Thank you @Michael. Your code looks much more neater. Object and classes are still quite confusing for me. I do a bit of php with mysql and javascript, but learning java and object orientated programming is something else. I did quite a few training now and still find it all this over-complicated and awkward, but I'm sure I'm going to like it much more once I get the hang of it.
@aya9 No problem. It's all practice.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.