我有一個ImageButton,我想用它來設置一個SQLiteDatabase中的項目的布爾值。響應SQLiteDatabase上的更新
ImageButton將顯示一個圖像的值爲「1」,另一個圖像的值爲「0」。按圖像按鈕應該切換數據庫字段,因此它的圖像。
出於某種原因,當我按下按鈕時,currentCursor.getInt()返回的值仍然相同,儘管在數據庫上調用了更新方法。我必須更新/刷新光標嗎?
在我的活動,我有:
private void updateFavouriteButton(){
int favourite = currentCursor.getInt(currentCursor.getColumnIndex(Object.favourite));
if (favourite == 1)
{
favouriteButton.setImageDrawable(getResources().getDrawable(R.drawable.favourite_selected));
favouriteButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
dbHelper.updateFavourite(selectedId, 0);
updateFavouriteButton();
}
});
}
else
{
favouriteButton.setImageDrawable(getResources().getDrawable(R.drawable.favourite));
favouriteButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
dbHelper.updateFavourite(selectedId, 1);
updateFavouriteButton();
}
});
}
}
在我的數據庫幫助我:
public void updateFavourite(long rowId, int favourite)
{
db.beginTransaction();
try {
ContentValues args = new ContentValues();
args.put("ZFAVOURITE", favourite);
int rowsAffected = db.update("ZOBJECT", args,"_id" + "=" + rowId, null);
if (rowsAffected > 0)
db.setTransactionSuccessful();
} catch (Exception e) {
Log.e("Error in transaction", e.toString());
} finally {
db.endTransaction();
}
}