开发者

Deciding the menu's action dynamically

I add menu's in my application dynamically depending on the values i get from the back-end in the onPrepareOptionsMenu(Menu menu). Now i would want to decide the action that is to be performed for the menu added dynamically (as i do not know the action to be performed be开发者_StackOverflow社区forehand) depending on certain values that came associated with the menu from the back-end. How do i achieve this. Kindly provide me some ideas on this. Thanks in advance.


Look into onCreateOptionsMenu and onPrepareOptionsMenu. Basically you need to overwrite them in your Activity and handle menus there. You can either remove or add menus in these overwridden methods.

Whole procedure is well-documented and described here


If you mean the option menu, then this guide should help you: Changing the Menu.

It shows how you can dynamically add and remove items from the menu right before it is shown to the user using the onPrepareOptionsMenu() method, which passes you the menu. You can then add or remove items from the menu.


You have to play around with onPrepareOptionsMenu and onCreateOptionsMenu. Here is good example how to do that.


I found a way to achieve what i wanted. I put the values associated to the menu as a part of the MenuItems intent. In the onOptionsItemSelected(MenuItem item) i get the intent from the MenuItem retrieve the values from it and perform the action required. The code is as below:

 @Override
public boolean onPrepareOptionsMenu(Menu menu) {
    // TODO Auto-generated method stub
    ApplicationManager.getInstance().resetCounter();
    menu.removeGroup(1);
    Vector listOfMenuToBeAdded=Service.getInstance().getMenuList();
    Enumeration<com.data.Menu> menuEnumeration = listOfMenuToBeAdded.elements();
    while (menuEnumeration.hasMoreElements()) {
        com.data.Menu levelOptions = (com.pravaa.mobile.dashboard.data.Menu) menuEnumeration
                .nextElement();
        MenuItem levelMenuItem = menu.add(1, 100, Menu.NONE,
                levelOptions.getCaption());
        LevelOptionIntent levelOptionIntent = new LevelOptionIntent();
        levelOptionIntent.setAppCode(levelOptions.getAppCode());
        levelOptionIntent.setDashboardNumber(levelOptions.getNumber());
        levelOptionIntent.setUniqueRecordId(levelOptions
                    .getUniqueRecordId());
        levelOptionIntent.setLevelNumber(levelOptions.getLevelNo());    
        levelMenuItem.setIntent(levelOptionIntent);
    }
    return true;
}

Once a menu item is clicked on perform the action according to the values set in the intent.

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // TODO Auto-generated method stub
    switch (item.getItemId()) {
    case 100:
        LevelOptionIntent levelOptionIntent = (LevelOptionIntent) item
                .getIntent();
        //Perform what you would want to based on the values set in the intent.
        if (levelOptionIntent.getAppCode().equals(A)) {
        // Start activity A
        }elseif (levelOptionIntent.getAppCode().equals(B)) {
        // Start activity B
        }else if (levelOptionIntent.getAppCode().equals(C)) {
        // Start activity C
        }

        break;
    }
    return true;
}

The LevelOptionsIntent class is as below:

public class LevelOptionIntent extends Intent {
private int dashboardNumber;
private String appCode;
private String uniqueRecordId;
private int levelNumber;

public LevelOptionIntent() {
    super();
    // TODO Auto-generated constructor stub
}
public int getDashboardNumber() {
    return dashboardNumber;
}
public void setDashboardNumber(int dashboardNumber) {
    this.dashboardNumber = dashboardNumber;
}
public String getAppCode() {
    return appCode;
}
public void setAppCode(String appCode) {
    this.appCode = appCode;
}
public String getUniqueRecordId() {
    return uniqueRecordId;
}
public void setUniqueRecordId(String uniqueRecordId) {
    this.uniqueRecordId = uniqueRecordId;
}
public int getLevelNumber() {
    return levelNumber;
}
public void setLevelNumber(int levelNumber) {
    this.levelNumber = levelNumber;
}}

I am not sure if this is the right way of doing it though.Can someone throw some light on this.

Thanks in advance.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜