2012-06-25 21 views
8

Tôi có một menu trong ứng dụng android của mình. Khi tôi bấm vào thêm yêu thích, tôi cần phải tải lại các tùy chọn trình đơn, làm cho nó xuất hiện yêu thích del trong các tùy chọn và không xuất hiện thêm yêu thích.làm thế nào để tải lại menu android trong cùng một hoạt động?

Tôi không muốn sử dụng lại hoạt động vì nút quay lại.

Mã của tôi:

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; 
} 

Cảm ơn!

Trả lời

36

sử dụng onPrepareOptionsMenu

Chuẩn bị trình đơn tùy chọn chuẩn của Màn hình sẽ được hiển thị. Điều này được gọi ngay trước khi menu được hiển thị, mỗi khi nó được hiển thị. Bạn có thể sử dụng phương pháp này để kích hoạt/vô hiệu hóa các mục hoặc tự động sửa đổi nội dung.

@Override 
public boolean onPrepareOptionsMenu(Menu menu) { 
    menu.clear(); 
    if(isChangedStat) { 
     menu.add(0, MENUITEM, 0, "True"); 
    } else { 
     menu.add(0, MENUITEM, 0, "False"); 
    } 
    return super.onPrepareOptionsMenu(menu); 
} 

Xin lưu ý hai điểm

1- Nếu có thể chỉ kích hoạt hoặc vô hiệu hóa mục trình đơn hoặc trông có thể trong trường hợp của bạn có thể thay đổi tiêu đề của cùng một trình đơn sẽ làm việc vì menu.clear(); có thể cần hơn sự chú ý trong khi xử lý

2- theo liên kết được cung cấp bởi Atlos

On 2.3.x Android và thấp hơn, hệ thống gọi onPrepareOptionsMenu() mỗi khi người dùng mở các tùy chọn menu (presses the Menu button).

Trên Android 3.0 trở lên, trình đơn tùy chọn được coi là luôn mở khi các mục menu được trình bày trong thanh tác vụ. Khi sự kiện xảy ra và bạn muốn thực hiện cập nhật menu, you must call invalidateOptionsMenu() để yêu cầu hệ thống gọi onPrepareOptionsMenu().

Các vấn đề liên quan