我的应用程序 android 中有一个菜单。当我单击添加收藏夹时,我需要重新加载菜单选项,使其在选项中显示为收藏夹而不显示添加收藏夹。
由于后退按钮,我不想使用重新加载活动。
我的代码:
public boolean onCreateOptionsMenu(Menu menu) {
try
{
MenuItem menuInicio = menu.add(INICIO, INICIO, 0, "Início");
menuInicio.setIcon(android.R.drawable.ic_menu_edit);
MenuItem menuBusca = menu.add(BUSCA, BUSCA, 0, "Buscar");
menuBusca.setIcon(android.R.drawable.ic_menu_search);
SubMenu menuFavoritos = menu.addSubMenu(FAVORITOS, FAVORITOS, 0, "Favoritos");
if(!phytoterapicContent.getPhytoterapicItem().getIsFav())
menuFavoritos.add(FAVORITOS, ADD_FAV, 0, "Adicionar aos Favoritos");
else
menuFavoritos.add(FAVORITOS, DEL_FAV, 1, "Remover dos Favoritos");
menuFavoritos.add(FAVORITOS, LIST_FAV, 2, "Listar Favoritos");
menuFavoritos.setIcon(android.R.drawable.star_off);
}
catch (Exception e) {
}
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case INICIO:
Intent it = new Intent(ShowPhytoterapicActivity.this, HomeActivity.class);
startActivity(it);
break;
case BUSCA:
Intent it3 = new Intent(ShowPhytoterapicActivity.this, ShowSearchParametersActivity.class);
startActivity(it3);
break;
case ADD_FAV:
try {
Dao<PhytoterapicItem, Integer> phytoterapicItemDao = getHelper().getPhytoterapicItemDao();
phytoterapicContent.getPhytoterapicItem().setIsFav(true);
phytoterapicItemDao.update(phytoterapicContent.getPhytoterapicItem());
Toast.makeText(ShowPhytoterapicActivity.this, "Adicionado aos Favoritos", Toast.LENGTH_LONG).show();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
case DEL_FAV:
try {
Dao<PhytoterapicItem, Integer> phytoterapicItemDao = getHelper().getPhytoterapicItemDao();
phytoterapicContent.getPhytoterapicItem().setIsFav(false);
phytoterapicItemDao.update(phytoterapicContent.getPhytoterapicItem());
Toast.makeText(ShowPhytoterapicActivity.this, "Removido dos Favoritos", Toast.LENGTH_LONG).show();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
case LIST_FAV:
Intent it5 = new Intent(ShowPhytoterapicActivity.this, ShowFavoritesActivity.class);
startActivity(it5);
break;
}
return true;
}
谢谢!