0

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;
    }
}
Saurabh Vardani
  • 1,821
  • 2
  • 18
  • 33
Nth2294
  • 3
  • 4

2 Answers2

0

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

Happy Coding :)

UserSharma
  • 458
  • 6
  • 20
0

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);
}
Niels Masdorp
  • 2,534
  • 2
  • 18
  • 31
  • Thank you! I tried immediately but ... it doesn't work :( it announced : java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference . – Nth2294 May 11 '16 at 14:15
  • That means that at the moment of comparing *get_dk1* the variable is still null. You can try moving the if else to the `postdata2()` method so you know the variable is set when you call `equals()` on it. You can find more info on `NullPointerExceptions` here: http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it – Niels Masdorp May 11 '16 at 14:23
  • I tried moving the if else to the `postdata2()` but it still doesn't work, this is my error : android.util.AndroidRuntimeException: Animators may only be run on Looper threads. So boring, I have tried to do all week :( – Nth2294 May 11 '16 at 14:49