當用戶按下ActionBar
上的圖標時,我正在更新SQLite數據庫中的記錄。正在更新的信息是一個將記錄添加到「收藏夾」頁面的標誌。根據條件更改ActionBar中的圖標
問題
當用戶添加或刪除記錄在您的收藏,我想在ActionBar
的圖標更改。我有一個完整的星形圖標和一個空的星形圖標。
setIcon
方法顯示如果記錄是收藏夾時的完整星形圖標,如果記錄不是收藏夾,則顯示空星形圖標。
在下面的代碼中,您將看到我使用的是boolean isInFavourite
,這是true
,當String fav = "y"
時。
當輸入Activity
時,顯示的icon
是正確的。
當用戶單擊icon
來調用onMenuItemClick()
方法時,記錄已成功更新,但icon
不會更改。
我無法改變boolean isInFavourite
當記錄已經更新因爲Eclipse希望所有的變量設置爲final
誰能幫我換一次記錄已被更新的圖標。
@Override
public boolean onCreateOptionsMenu(Menu menu) {
db = new DBHelper(this);
db.createDataBase();
db.openDataBase();
Bundle bundle = getIntent().getExtras();
final String rowid = bundle.getString("id");
final String fav = bundle.getString("fav");
//Boolean to check if record is a favourite
final boolean isInFavourite = fav.contentEquals("y");
menu.add("Add to Favourites")
.setOnMenuItemClickListener(new OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
String toggle;
String message;
//Logic to add or remove row from recording.
if (isInFavourite) {
toggle = "n";
message = "Recipe removed from Favourites";
} else {
toggle = "y";
message = "Recipe added to Favourites";
}
//Update favourite record in database
db.updateFavourite(rowid, toggle);
db.close();
Toast.makeText(getApplicationContext(), message,
Toast.LENGTH_LONG).show();
return true;
}
})
//Set icon depending on whether record is a favourite or not.
.setIcon(isInFavourite ? R.drawable.fav_true : R.drawable.fav_false)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
return true;
}
感謝@dmon爲解決
SOLUTION
private DBHelper db = null;
public String fav = null;
public String rowid = null;
public boolean isFav;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
Bundle bundle = getIntent().getExtras();
rowid = bundle.getString("id");
fav = bundle.getString("fav");
if (fav.contentEquals("y")) {
isFav = true;
} else {
isFav = false;
}
try {
db = new DBHelper(this);
db.createDataBase();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getSupportMenuInflater().inflate(R.menu.menu_settings, menu);
return true;
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem fave = menu.findItem(R.id.add);
MenuItem unfave = menu.findItem(R.id.remove);
fave.setVisible(isFav);
unfave.setVisible(!isFav);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
case R.id.add:
fav = "n";
isFav = false;
updateFav();
supportInvalidateOptionsMenu();
Toast("Removed from Favourites");
return true;
case R.id.remove:
fav = "y";
isFav = true;
updateFav();
supportInvalidateOptionsMenu();
Toast("Added to Favourites");
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void updateFav(){
db.openDataBase();
db.updateFavourite(rowid, fav);
db.close();
}
XML文件:RES /菜單/ menu_settings.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/add"
android:icon="@drawable/good"
android:showAsAction="always"
/>
<item
android:id="@+id/remove"
android:icon="@drawable/bad"
android:showAsAction="always"/>
我需要讓我的自己的XML文件的菜單項的當前狀態的全局變量? – tiptopjat 2012-08-08 07:09:39
不一定,你可以按照你一直這樣做的方式來做,而不是膨脹它在飛行中創建它。這就是說,XML更清潔。 – dmon 2012-08-08 14:04:40
試圖使用您的解決方案。什麼方法應該有我的數據庫邏輯?我發現使用java而不是xml創建菜單更容易。 – tiptopjat 2012-08-08 22:52:29