对话框android里面的List视图的上下文菜单

时间:2012-02-28 10:48:19

标签: android listview dialog contextmenu alertdialog

我有自定义对话框,其中包含列表视图。我希望在长按对话框中的列表项时出现上下文菜单。出现上下文菜单但是当我点击任何项目时没有任何反应。我已经提供了在单击上下文菜单项但没有任何操作时要执行的操作。任何人都可以帮忙吗?

     final ArrayList<ListClass> listItem = coreData_.listItem_;

    LayoutInflater inflater = (LayoutInflater) 
             GUI.this.getSystemService(LAYOUT_INFLATER_SERVICE);
    View layout = inflater.inflate(R.layout.scanlist,
                    (ViewGroup) findViewById(R.id.scan_dialog));
    AlertDialog.Builder  builder = 
             new AlertDialog.Builder(GUI.this);
    builder.setView(layout);

    scanListView_ = (ListView) 
            layout.findViewById(R.id.scan_list_view);

    registerForContextMenu(scanListView_);

    scanListView_.setOnCreateContextMenuListener(this);

    scanListView_.setBackgroundColor(Color.rgb(0, 0, 0));               

    scanListView_.setAdapter(
            new EfficientAdapter(getApplicationContext(),
                        listItem));

    scanListView_.setOnItemClickListener(new OnItemClickListener(){

        public void onItemClick(AdapterView<?> arg0, View arg1, 
                int position, long arg3) {

            //perform list item click actions
        }

    });

    builder.setNegativeButton("Cancel", 
            new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub
            //dismiss
        }
    });

    availNetDialog_ = builder.create();
    availNetDialog_.setTitle("Available Networks");
    availNetDialog_.show();

1 个答案:

答案 0 :(得分:1)

public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    // second argument show what item was selected
    menu.add(0, 0, 1, "Delet Row").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            if (item.getItemId() == 0) {
                // get item id from listView if needed
                AdapterView.AdapterContextMenuInfo acmi = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
                // extract id and transfer it to another method delRec
                delRec(acmi.id);
                //init();  
                return true;
            }
            return false;
        }
    });
}

总结一下,只需setOnMenuItemClickListener和@Override onMenuItemClick。 希望它有所帮助:)