2 回答
TA贡献1806条经验 获得超5个赞
要保存复选框的状态,请使用SharedPefrences
//globally declare it
SharedPreferences.Editor editor;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.filters);
editor = getSharedPreferences("STATE", MODE_PRIVATE).edit();
SharedPreferences settings =
PreferenceManager.getDefaultSharedPreferences(this);
boolean state = settings.getBoolean("isChecked", false);
if(state){
//set the checkbox checked
}else{
//set the checkbox Unchecked
}
}
}
else if (id == R.id.favourites){
if (item.isChecked()) {
item.setChecked(false);
editor.putBoolean("isChecked", false)
editor.apply();
}
else {
item.setChecked(true);
editor.putBoolean("isChecked", true)
editor.apply();
}
return true;
}
更新数据库使用这个
public int Update(int id) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(Note.COLUMN_NOTE, "Yes");
// updating row
return db.update(Note.TABLE_NAME, values, Note.COLUMN_ID + " = ?",
new String[]{String.valueOf(id)});
在 onCreateOptionMenu 上执行此操作
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
//here fetch the data from shared prfrencess which done on oncrete
//use if else here
menu.findItem(R.id.two_week).setChecked(checked);
return true;
}
添加回答
举报