Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android RatingBar onRatingChanged Listener

I am using a RatingBar element in my app, and im trying to set it up with an OnRatingChangedListener. In the listener, im doing different stuff using if statements on a variable. Problem is, for some reason when i have multiple options this listener isn't working. It is setup in the onCreate() method, before the variable in the if statements is getting a value, but i tried to move it to a different location with no success at all.

My onCreate():

bar = (RatingBar) findViewById(R.id.serviceLevel);
    bar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {

        @Override
        public void onRatingChanged(RatingBar ratingBar, float rating,
                boolean fromUser) {

        if(Code==null){
            Toast.makeText(TipCalculatorActivity.this, "Locationing error occured, please report to the developer.", Toast.LENGTH_LONG).show();
        }else if(Code=="1"){
            if(rating==0){
                autochange.setText("Precentage: 0%");
                precentage = 0;
                calculate(value, precentage);
            }else if(rating==0.5){
                autochange.setText("Precentage: 10%");
                precentage = 8;
                calculate(value, precentage);
            }else if(rating==1){
                autochange.setText("Precentage: 11%");
                precentage = 9;
                calculate(value, precentage);
            }else if(rating==1.5){
                autochange.setText("Precentage: 11%");
                precentage = 10;
                calculate(value, precentage);
            }else if(rating==2){
                autochange.setText("Precentage: 12%");
                precentage = 11;
                calculate(value, precentage);
            }else if(rating==2.5){
                autochange.setText("Precentage: 12%");
                precentage = 12;
                calculate(value, precentage);
            }else if(rating==3){
                autochange.setText("Precentage: 13%");
                precentage = 13;
                calculate(value, precentage);
            }else if(rating==3.5){
                autochange.setText("Precentage: 13%");
                precentage = 14;
                calculate(value, precentage);
            }else if(rating==4){
                autochange.setText("Precentage: 14%");
                precentage = 15;
                calculate(value, precentage);
            }else if(rating==4.5){
                autochange.setText("Precentage: 14%");
                precentage = 16;
                calculate(value, precentage);
            }else if(rating==5){
                autochange.setText("Precentage: 15%");
                precentage = 17;
                calculate(value, precentage);
            }
        }else if(Code=="2"){
            if(rating==0){
                autochange.setText("Precentage: 0%");
                precentage = 0;
                calculate(value, precentage);
            }else if(rating==0.5){
                autochange.setText("Precentage: 10%");
                precentage = 10;
                calculate(value, precentage);
            }else if(rating==1){
                autochange.setText("Precentage: 11%");
                precentage = 11;
                calculate(value, precentage);
            }else if(rating==1.5){
                autochange.setText("Precentage: 12%");
                precentage = 12;
                calculate(value, precentage);
            }else if(rating==2){
                autochange.setText("Precentage: 13%");
                precentage = 13;
                calculate(value, precentage);
            }else if(rating==2.5){
                autochange.setText("Precentage: 14%");
                precentage = 14;
                calculate(value, precentage);
            }else if(rating==3){
                autochange.setText("Precentage: 15%");
                precentage = 15;
                calculate(value, precentage);
            }else if(rating==3.5){
                autochange.setText("Precentage: 16%");
                precentage = 16;
                calculate(value, precentage);
            }else if(rating==4){
                autochange.setText("Precentage: 17%");
                precentage = 17;
                calculate(value, precentage);
            }else if(rating==4.5){
                autochange.setText("Precentage: 18%");
                precentage = 18;
                calculate(value, precentage);
            }else if(rating==5){
                autochange.setText("Precentage: 19%");
                precentage = 19;
                calculate(value, precentage);
            }
        }
    }
});

My Code variable is set after an AsyncTask is done, so probably after the UI is generated. Any solution?

like image 645
arielschon12 Avatar asked Sep 28 '12 19:09

arielschon12


1 Answers

In order to make your code work you'll have to use the equals() method to check if Code has the right value:

// ...
} else if (Code.equals("1")){
// ...

== will test the references and will return false as the two references don't point to the same object.

like image 168
user Avatar answered Oct 08 '22 16:10

user