Contextmenu无法显示

时间:2016-03-07 12:43:06

标签: android contextmenu

我试图在LongClick Context Menu单元格上显示listView,但Contextmenu无法显示... 无法弄清楚原因?
这是我的onCreateonCreateContextMenu方法...
我几乎尝试了所有的事情......
小代码会有所帮助...

 @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.content_after_register);

        storeRegistrationDataBase = new StoreRegistrationDataBase(this);
        storeRegistrationDataBase = storeRegistrationDataBase.open();

        insertItemButton = (Button) findViewById(R.id.button4);
        listView = (ListView) findViewById(R.id.listView);
        registerForContextMenu(listView);
    }



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

        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_main, menu);
    }

menu.xml文件

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/edit"
        android:title="@string/edit">
    </item>
    <item
        android:id="@+id/delete"
        android:title="@string/delete">
    </item>

</menu>

3 个答案:

答案 0 :(得分:1)

你必须做这样的事情..

@Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Countries = getResources().getStringArray(R.array.countries);
        Arrays.sort(Countries);

        ListView listView = (ListView) findViewById(R.id.listview);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.listitem, R.id.aaa, Countries);
        listView.setAdapter(adapter);
        registerForContextMenu(listView);
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo)
    {
        if(v.getId() == R.id.listview)
        {
            AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
            menu.setHeaderTitle(Countries[info.position]);
            String[] menuItems = getResources().getStringArray(R.array.menu);

            for(int i = 0; i < menuItems.length; i++)
            {
                menu.add(Menu.NONE, i, i, menuItems[i]);
            }
        }
    }

    @Override
    public boolean onContextItemSelected(MenuItem item)
    {
        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
        int menuItemIndex = item.getItemId();
        String[] menuItems = getResources().getStringArray(R.array.menu);
        String menuItemName = menuItems[menuItemIndex];
        String listItemsName = Countries[info.position];

        TextView text = (TextView) findViewById(R.id.textView);
        text.setText(String.format("Select %s for item %s", menuItemName, listItemsName));
        return true;
    }

答案 1 :(得分:0)

尝试实现setOnLongClickListener,返回false。

yourlistview.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            return false;
        }
    });

答案 2 :(得分:0)

为什么不删除这些内容:

menu.add(0, v.getId(), 0, "Edit");//groupId, itemId, order, title
menu.add(0, v.getId(), 0, "Delete");

并在 R.menu.menu_main xml文件中添加两个菜单项。

在这里,您要添加两个具有相同ID的菜单项。