3

I have a variable that I get from shared preferences when I load the app.

I first initialize the variable

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    final String camera_type = booth_preferences.getString("camera_key", "back");

Then later down the line, I get that variable and do something with it

if(camera_type.equals("front")){
        //do something
} else if(camera_type.equals("ext")){
        //do something
} else {
        //do something
}

Now, directly after that if statement, I have an onclick listener that is supposed to change and update that preference.

camera_button_front.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        //do something
        edit_preferences.putString(camera_key, "front").commit();
    }
});
camera_button_back.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        //do something
        edit_preferences.putString(camera_key, "back").commit();
    }
});
camera_button_ext.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        //do something
        edit_preferences.putString(camera_key, "ext").commit();
    }
});

But when I try to change the variable camera_type I get errors stating "cannot assign a value to final variable 'camera_type'".

camera_button_ext.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        //do something
        edit_preferences.putString(camera_key, "ext").commit();
        camera_type = "ext";
    }
});

I've ever reinstated the variable after the onclick hoping it would over write the variable completely.

camera_button_ext.setOnClickListener(new View.OnClickListener() {
    String camera_type;
    public void onClick(View v) {
        //do something
        edit_preferences.putString(camera_key, "ext").commit();
        camera_type = "ext";
    }
});

If I were to remove the final then I'm not able to use the variable in the if statement.

I'm new to Java, so this should be a simple answer, I'm just not sure which combination of wrong I'm doing.

3 Answers 3

2

You should make your variable a non-final field. Then you can use it within the onClick (and all other methods of the class) and also reassign it as you wish.

private String camera_type; // member variable

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    camera_type = booth_preferences.getString("camera_key", "back");
Sign up to request clarification or add additional context in comments.

Comments

2

You cannot reassign value to a final variable. To make it accessible inside your onClick method, you could make camera_type a member variable of your activity.

Comments

0

You can make one element massive

String[] cameraType = new String[0];

and use

if("some_String".equals(cameraType[0])){
//do smth
}

Comments

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.