0

=========================================================================== Thank you for your answers! I changed my codes and I added some TextViews to check if my coding works(if variables change correctly), but I can't see any value under "LED in/off" "Relay" and "LED blink". What is the problem with this? :(

private TextView showpin13, showpin5, showLEDblink;


private String pin13 = "";
private String pin5 = "";
private String LEDblink = "";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_change_status);

    switchPin13 = (Switch) findViewById(R.id.changeLED1);
    switchBlinkPin13 = (Switch) findViewById(R.id.changeLED2);
    switchPin5 = (Switch) findViewById(R.id.changeRELAY);
    viewBlink = (TextView) findViewById(R.id.textView7);

    //임시
    showpin13 = (TextView) findViewById(R.id.showpin13);
    showpin5 = (TextView) findViewById(R.id.showpin5);
    showLEDblink = (TextView) findViewById(R.id.showLEDblink);


    viewBlink.setVisibility(View.INVISIBLE);
    switchBlinkPin13.setVisibility(View.INVISIBLE);


    switchPin13.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (switchPin13.isChecked()) {
                switchBlinkPin13.setVisibility(View.VISIBLE);
                viewBlink.setVisibility(View.VISIBLE);
                pin13 = "1";
            } else {
                pin13 = "0";
                viewBlink.setVisibility(View.INVISIBLE);
                switchBlinkPin13.setVisibility(View.INVISIBLE);
                switchBlinkPin13.setChecked(false);
            }
        }
    });

    switchPin5.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (switchPin5.isChecked()) {
                pin5 = "1";
            } else {
                pin5 = "0";
            }
        }
    });

    switchBlinkPin13.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if(switchBlinkPin13.isChecked()){
                LEDblink = "1";
            }
            else{
                LEDblink = "0";
            }
        }
    });


    String pinnumber13 = pin13;

    showpin13.setText(String.valueOf(pinnumber13));
    showpin5.setText(String.valueOf(pin5));
    showLEDblink.setText(String.valueOf(LEDblink));
}

enter image description here

  • you must define your `pin13` `final`, but you won't be able to change it so better way would be to create a `setter` for this variable `private void setPin13(String pin13) { this.pin13 = pin13 }`, of course your `pin13` must be defined as class attribute ... alternatively you can do this for your `Switch` instances – miskohut Jan 23 '16 at 14:56

3 Answers3

0

the first answer would be make String Pin13 a global variable (i.e declare it out of onCreate() inside the class) the answer to the second point would depend upon what do you want accomplish do you want to toggel it to off that is pin.setChecked(false)

Avinash Joshi
  • 1,307
  • 8
  • 25
0
  1. put Sting pin13 outside onCreate()
  2. inside the else just add switchBlinkPin13.setChecked(false);
Mo1989
  • 2,374
  • 1
  • 15
  • 17
  • Thank you for your answer and I changed my code but I think the variables are not changing... – He Won Cho Jan 23 '16 at 16:35
  • @He: the textViews as you have them in your code are never updated.... you need to call set text every time you set the value of the variable... just updating the variable will not update the textview – Mo1989 Jan 24 '16 at 08:09
0
String pin13="";//CHANGE HERE
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_change_status);

switchPin13 = (Switch)findViewById(R.id.changeLED1); //LED switch
switchBlinkPin13 = (Switch)findViewById(R.id.changeLED2); //LED blink
switchPin5 = (Switch)findViewById(R.id.changeRELAY); //Relay switch

switchBlinkPin13.setVisibility(View.INVISIBLE);

final String pin13=""; //or change here 

switchPin13.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if(switchPin13.isChecked()){
            switchBlinkPin13.setVisibility(View.VISIBLE);
            pin13 = "1"; //I cannot use pin13 here.
        }
        else {
            pin13 = "0"; //I cannot use pin13 here.
            switchBlinkPin13.setVisibility(View.INVISIBLE);
            //How to set switchBlinkPin13 "OFF"?
        }
    }

However, you might want to use VIEW.GONE instead of INVISIBLE. When, your item is invisible, it will take some spaces in your LayoutUI, so there will be a useless space in the screen. If you use GONE (you can get it back, don't worry), you won't face it. and when your blinker button is GONE, you won't have to worry about it's onClickListener.