简体   繁体   中英

Android cannot show menu drop down when click

I want to show menu list when click listview item, I can get the object, but failed to show the menu. Can you please take a look at my code? Thanks!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM