设置切换视图状态,根据数据库值android进行检查

时间:2014-12-12 18:24:11

标签: android sqlite android-switch

我今天刚刚开始学习安卓,到目前为止,我的情况很好,但在动态改变切换视图的状态时遇到了问题。我已经设置了检查更新的选项,以便用户可以根据自己的选择打开或关闭它。

我正在尝试根据存储在数据库中的最后选择的用户选项将切换视图的状态设置为选中或取消选中,代码为:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_setting);
    sw = (Switch) findViewById(R.id.switchupdates);

    taskkeeperdb = new DbHelper(this, "taskkeeper", null, 1);
    db = taskkeeperdb.getReadableDatabase();
    Cursor result = db.rawQuery("select * from tblsettings", null);
    System.out.println("Cursore Created");
    if (result != null) {
        System.out.println("While Block");
        while (result.moveToNext()) {
            System.out.println("inside while block");
            String name = result.getString(result.getColumnIndex("title"));
            String value = result.getString(result.getColumnIndex("value"));
            System.out.println("values are in variable");
            switch (name) {
            case "check_updates":
                System.out.println("check updae case");
                if (value == "YES") {
                    //sw.setText("Check for Updates: On");
                    sw.setChecked(true);
                    System.out.println("YESYESYESYESYESYESYESYESY");
                } else {
                    //sw.setText("Check for Updates: Off");
                    sw.setChecked(false);
                    System.out.println("NONONONONONONONONON");
                }
                break;
            default:
                break;
            }
        }
        System.out.println("ending the if block.....");
    }
    if (!result.isClosed()) {
        result.close();
    }
    sw.setOnCheckedChangeListener(this);

}

当用户与交换机视图切换交互时更新表.....

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    // TODO Auto-generated method stub
    db = taskkeeperdb.getWritableDatabase();
    ContentValues cv = new ContentValues();
    if (isChecked) {
        cv.put("value", "YES");
        db.update("tblsettings", cv, "title='check_updates'", null);
        sw.setText("Check for Updates: On");
        cv.clear();
    } else {
        cv.put("value", "NO");
        db.update("tblsettings", cv, "title='check_updates'", null);
        sw.setText("Check for Updates: Off");
        cv.clear();
    }
}

现在,当我打开设置视图时,它不会反映更改。 在我的控制台中,system.out.println仅显示:

12-12 13:11:59.810: I/System.out(23320): Cursore Created

12-12 13:11:59.810: I/System.out(23320): While Block

12-12 13:11:59.820: I/System.out(23320): ending the if block.....

我的DbHelper代码:

public DbHelper(Context context, String databaseName,
        CursorFactory factory, int version) {
    super(context, DATABASE_NAME, null, 1);
    // TODO Auto-generated constructor stub
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("create table if not exists tblsettings (id integer primary key autoincrement, title text, value text);");
    Cursor result = db.rawQuery("select * from tblsettings", null);
    //when no rows then insert fresh rows..
    if (result.getCount() == 0) {
        ContentValues contentValues = new ContentValues();
        contentValues.put("title", "check_updates");
        contentValues.put("value", "NO");
        db.insert("tblsettings", null, contentValues);
    }
    if (!result.isClosed()) {
        result.close();
    }
}

因为我是新手,所以我无法理解可能是什么原因。 请提前帮助谢谢

1 个答案:

答案 0 :(得分:1)

感谢大家,我已经解决了这个问题,解决方案只是一个小错误..

通过更改check_updates ==中的value.toString().equals("YES")switch case的值比较,我得到了我想要的内容..更新的switch case现在是:

switch (name) {
 case "check_updates":
         if (value.toString().equals("YES")) {
            sw.setText("Check for Updates: On");
            sw.setChecked(true);
         } else {
             sw.setText("Check for Updates: Off");
             sw.setChecked(false);
         }
         break;
 default:
         break;
            }