简体   繁体   中英

Getter and Setter in Android

In my MainActivity I have a Switch and a Button, After changing the switch status, Once the button is clicked I want to get the switch status true/false. To do this, I've implemented Getter and Setter, for unknown reason, once I set the value, i'm always getting false.

MainActivity

 Switch profession = (Switch) findViewById(R.id.profswitch);
profession.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        GetSwitch SP = new GetSwitch();
        SP.setSetClicked(isChecked);
    }
});

Button profButton = findViewById(R.id.btnSwitchNxt);
profButton.setOnClickListener(new View.OnClickListener() {
   @Override
    public void onClick(View v) {
       GetSwitch sw = new GetSwitch();
       boolean setClicked = sw.isSetClicked();
       Log.i("GetSwitch",setClicked+"");

   }
});

GetSwitch

public class GetSwitch{

    private boolean setClicked;

    public void setSetClicked(boolean setClicked) {
        this.setClicked = setClicked;
        Log.i("SetSwitch",this.setClicked+"");
    }

    public boolean isSetClicked() {
        return setClicked;
    }
}

What am I doing wrong here?

You are using different object for setting and getting value. You just need to declare one GetSwitch object, which has to be like below..

   GetSwitch SP = new GetSwitch();
   Switch profession = (Switch) findViewById(R.id.profswitch);
   profession.setOnCheckedChangeListener(new 
   CompoundButton.OnCheckedChangeListener() {
       @Override
       public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {      
           SP.setSetClicked(isChecked);
       } 
   }); 

   Button profButton = findViewById(R.id.btnSwitchNxt);
   profButton.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View v) {      
           boolean setClicked = SP.isSetClicked();
           Log.i("GetSwitch",setClicked+"");
       }
   });

You are using two separate instances of the same class for getting same property. When you are updating a property of an instance that change belongs to that instance. You can do something like this:

Switch profession = (Switch) findViewById(R.id.profswitch);
GetSwitch sw = new GetSwitch();
profession.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        sw.setSetClicked(isChecked);
    }
});

Button profButton = findViewById(R.id.btnSwitchNxt);
profButton.setOnClickListener(new View.OnClickListener() {
   @Override
    public void onClick(View v) {
       boolean setClicked = sw.isSetClicked();
       Log.i("GetSwitch",setClicked+"");
   }
});

See here I am getting and setting the property of same instance so that they are not changed. You can also define this instance in the constructor of the class and reference them using this.sw as a good practice.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM