简体   繁体   中英

How I can change state of widget switch from check to uncheck?

I want to recieve data from thingspeak and change automatically state of switch from checked to unchecked or vice versa.
This is my code, but it doesn't work, just can set text but can't change state.
Please help me!

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

        button = (Button) findViewById(R.id.button);

        myswitch1 = (Switch) findViewById(R.id.switch1);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Thread t = new Thread(new Runnable() {
                    @Override
                    public void run() {
                        postdata2();
                    }
                });
                t.start();
                now_da1.setText(get_da1);
                if(get_dk1 == "1") {
                      myswitch1.setChecked(False);
                }   
                else if (get_dk1 =="0") {
                      myswitch1.setChecked(True);
                }
            }
        });

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

        public void postdata2() {
        HttpRequest mReq = new HttpRequest();
        get_t1 =    mReq.sendGet("https://thingspeak.com/channels/106453/field/1/last");

        get_da1 = get_t1.substring(0, 2);
        get_dk1 = get_t1.substring(3, 3);
        Log.d(myGet, get_dk1);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

use this myswitch1.setChecked(false); to uncheck and myswitch1.setChecked(true); to make it checked.

Happy Coding :)

When comparing the value of two Strings always use the equals() method which compares the values instead of using == which compares the object references .

For example:

String first = "1";
String second = "1";

first == second will return false because they are not referencing to the same object. However, first.equals(second) will return true because the value of the two Strings are identical.

You can try replacing the conditions with this:

if (get_dk1.equals("1")) {
    myswitch1.setChecked(false);
} else if (get_dk1.equals("0")) {
    myswitch1.setChecked(true);
}

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