title: how to save toggle button state to database and if user edits then edited state should be saved to database in android
body: <p>how to save toggle button state to database and if user edits then edited state should be saved to database in android.the following is my code it is working fine.but the problem is that when user edits to <strong>on</strong> it is in on state and <strong>green light</strong> displays on toggle button. and then user performs the required actions.when user again comes back to edit again it displays <strong>on</strong> only but <strong>no green light</strong> is visible.so it looks bad so please help me and following is my code.</p>
  
  <pre><code>    protected void onCreate(Bundle savedInstanceState) {
                  super.onCreate(savedInstanceState);
                  setContentView(R.layout.edit_layout);
                  edittext=(EditText)findViewById(R.id.device_text);
                  light=(ToggleButton)findViewById(R.id.light);
                  alarm=(ToggleButton)findViewById(R.id.alarm);
                  db = new DataBaseAdapter(this);
  
                   Intent i = getIntent();
  
  
                     if(i.hasExtra("Dname"))
                           val = i.getStringExtra("Dname");
  
  
                     if(i.hasExtra("Dlight"))
                     slight=i.getStringExtra("Dlight");
                     blight=Boolean.valueOf(slight);
  
  
                     if(i.hasExtra("Dalarm"))
                     salarm=i.getStringExtra("Dalarm");
                     balarm=Boolean.valueOf(balarm);
                  if(i.hasExtra("Daddress"))
                      pos=i.getStringExtra("Daddress");
  
  
                db.open();                  
                db.insertData(pos,val,slight,salarm);
  
                   c = db.getData();
                   edittext.setText(val);
                   light.setText(slight);
                  // light.setChecked(blight);
                   alarm.setText(salarm);
                   //alarm.setChecked(balarm);
  
                      db.close();
               }
  
  
                  @Override
                  public void onBackPressed(){
                      db.open();
                      c=db.getData();
  
                      if (c.moveToFirst()) {
                          do {
  
  
                                  String strSQL = "UPDATE DeviceDetails SET devicename ='"+ edittext.getText().toString() +"'  WHERE uuid = '"+c.getString(c.getColumnIndex("uuid"))+"'" ;
                                  db.select(strSQL);
  
                                  slight=light.getText().toString();
                                  salarm=alarm.getText().toString();
  
  
                                  if(pos.equalsIgnoreCase(c.getString(c.getColumnIndex("uuid"))))
                                  {
                                  db.updateData(pos, edittext.getText().toString(),slight,salarm);
                                  }
  
                             Intent intent=new Intent();
                              intent.putExtra("Dname", edittext.getText().toString());
                              intent.putExtra("Daddress",pos);
                              intent.putExtra("Dlight", slight);
                              intent.putExtra("Dalarm", salarm);
                              setResult(RESULT_OK, intent);
                              finish();
                      } while (c.moveToNext());
                      }
  
                  db.close();
  
                  super.onBackPressed();
                  }  
  }
  </code></pre>