单击时,Android无法显示菜单下拉菜单

时间:2014-07-10 19:58:07

标签: java android

我想在点击listview项目时显示菜单列表,我可以获取该对象,但无法显示菜单。你能看一下我的代码吗?谢谢!

  public View onCreateView

    (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        mActivity = this.getActivity();

        View root = inflater.inflate(R.layout.orders, container, false);

        adapter = new OrdersItemAdapter(root.getContext(), R.layout.order_row);

        final ListView list = (ListView) root.findViewById(R.id.order_list);

        list.setAdapter(adapter);

        mActivity.registerForContextMenu(list);

        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
               // Toast.makeText(parent.getContext(), "view clicked: " + arr[0], Toast.LENGTH_SHORT).show();
                mActivity.registerForContextMenu(list);
                mActivity.openContextMenu(view);
                view.showContextMenu();
                String str = list.getItemAtPosition(position).toString();
                System.out.println(str);
            }
        });
        refresh();
        return root;
    }
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        this.onCreateContextMenu(menu, v, menuInfo);
        System.out.println("...on create context android.view.MenuItem...");
        menu.add(0, v.getId(), 0, "Action 1");
        menu.add(0, v.getId(), 0, "Action 2");
    }

1 个答案:

答案 0 :(得分:0)