如何在数据库中保存切换按钮状态并检索该状态

时间:2014-03-25 06:18:34

标签: android

如何保存切换按钮状态并检索该状态,并应在应用中访问。 以下是我的编辑类,我想插入值并更新切换按钮状态。并没有保存在数据库中并进行更新。

    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");
    slight=light.getText().toString();
    salarm=alarm.getText().toString();
//  edittext.setText(val);
    if(i.hasExtra("Daddress"))
        pos=i.getStringExtra("Daddress");
    db.open();
    db.insertData(pos,val,slight,salarm);
    c = db.getData();
    edittext.setText(val);
    db.close();
}

@Override
public void onBackPressed(){
    db.open();
    c=db.getData();
    if (c.moveToFirst()) {
      do {
        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);
        setResult(RESULT_OK, intent);
        finish();
    } while (c.moveToNext());
          }
     db.close();
     super.onBackPressed();
}   

以下是我的数据库适配器,我这样做是为了插入和更新

   public long insertData(String uuid,String devicename ,String light,String alarm ) 
   {
        ContentValues initialValues = new ContentValues();
        initialValues.put(UUID, uuid);
        initialValues.put(DEVICENAME, devicename);
        initialValues.put(LIGHT, light);
        initialValues.put(ALARM, alarm);
        return db.insert(DATABASE_TABLE, null, initialValues);
  }

 public long updateData(String uuid,String devicename,String light,String alarm  )
  {
    ContentValues initialValues = new ContentValues()
        initialValues.put(DEVICENAME, devicename);
    initialValues.put(LIGHT, light); 
    initialValues.put(ALARM, alarm);
    return db.update(DATABASE_TABLE, initialValues,UUID + "= ?",new String[] { uuid});
}

2 个答案:

答案 0 :(得分:0)

尝试setOnCheckedChangeListener并根据切换按钮状态

将值添加到数据库中

//这会在更改时提供切换按钮的状态

light.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    if (isChecked==true) 
{
//do your operations here
}
else
{
}

您可以尝试使用共享偏好设置,如下所示

保存:

@Override
public void onClick(View v) 
{
    if (toggle.isChecked()) 
    {
        SharedPreferences.Editor editor = getSharedPreferences("com.example.xyz", MODE_PRIVATE).edit();
        editor.putBoolean("NameOfThingToSave", true);
        editor.commit();
    }
    else
    {
        SharedPreferences.Editor editor = getSharedPreferences("com.example.xyz", MODE_PRIVATE).edit();
        editor.putBoolean("NameOfThingToSave", false);
        editor.commit();
    }
}

To load:

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    SharedPreferences sharedPrefs = getSharedPreferences("com.example.xyle", MODE_PRIVATE);
    toggle.setChecked(sharedPrefs.getBoolean("NameOfThingToSave", true));
}

答案 1 :(得分:0)

如果您的数据非常小,请使用SharedPreferences代替Sqlite。有关如何使用SharedPreferences

,请参阅this