打开上下文菜单,点击上下文菜单选项

时间:2014-05-18 09:00:46

标签: android contextmenu

我想在上下文菜单中列出食物类型,子类型和食物。这非常有效。但是当我选择一个选项时,我想重新打开上下文菜单并加载不同的选项。问题是,在我选择了一个选项后,上下文菜单会重新打开新选项,但会立即关闭。

我的代码:

// This opens my context menu, works perfectly
public void onClick(View v)
{
    Intent intent = new Intent(getBaseContext(), OrderInfo.class);

    thisView = v;
    registerForContextMenu(v); 
    openContextMenu(v);
}

@Override  
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {  
    super.onCreateContextMenu(menu, v, menuInfo);

            // When this first opens, the value of contextOption is 0. 
            // This changes when I choose an option in the context menu
            // I store the displayable data in Hashmaps
    switch(contextOption)
    {
        case 0:
        {
            menu.setHeaderTitle("Food Types");
            for (final Map.Entry typeEntry : MainActivity.food_types.entrySet()) {
                final int Key = Integer.parseInt((String) typeEntry.getKey());
                String data = (String) typeEntry.getValue();
                String[] parts = data.split(";");
                menu.add(0, v.getId(), 0, parts[0]);
            }
        }
        break;
        case 1:
        {
            menu.setHeaderTitle("Food Subtypes");
            for (final Map.Entry typeEntry : MainActivity.food_subtypes.entrySet()) {
                final int Key = Integer.parseInt((String) typeEntry.getKey());
                String data = (String) typeEntry.getValue();
                String[] parts = data.split(";");
                menu.add(0, v.getId(), 0, parts[1]);
            }
        }
        break;
        case 2:
        {
            menu.setHeaderTitle("Foods");
            for (final Map.Entry typeEntry : MainActivity.foods.entrySet()) {
                final int Key = Integer.parseInt((String) typeEntry.getKey());
                String data = (String) typeEntry.getValue();
                String[] parts = data.split(";");
                menu.add(0, v.getId(), 0, parts[1]);
            }
        }
        break;
    }
}  
@Override  
public boolean onContextItemSelected(MenuItem item) {  

    boolean foundValue = false;

    switch(contextOption)
    {
        case 0:
        {
            for (final Map.Entry typeEntry : MainActivity.food_types.entrySet()) {
                final int Key = Integer.parseInt((String) typeEntry.getKey());
                String data = (String) typeEntry.getValue();
                String[] parts = data.split(";");

                if(item.getTitle().equals(parts[0]))
                {
                    foundValue = true;
                    if(parts[1].equals("1"))
                    {
                                            // if the food type has subtypes
                        contextOption = 1;
                        contextChosenID = Key;
                    }
                    else
                    {
                        contextOption = 2;
                        contextChosenID = Key;
                    }
                }
                else 
                {

                }
            }
        }
        break;
        case 1:
        {
            // Stuff happens
        }
        break;
        case 2:
        {
            // Stuff happens
        }
        break;
    }

    // I put this here because I want to run the open command anyway, just for the testing
    openContextMenu(thisView); // This runs, but after it opened the context menu again with the new data it closes immediately
    return false;  
}

你能给我一些如何解决的建议吗?

1 个答案:

答案 0 :(得分:1)

我怀疑问题是仍有待处理的事件会在代码运行后关闭上下文菜单。将代码放在Runnable中显示新的上下文菜单并将其发布到Handler会延迟执行代码,直到上下文菜单关闭,从而允许它保持打开状态。