Vikram Gupta
Vikram Gupta

Reputation: 6516

context menu for List view inside dialog box android

I have custom dialog box which contains a list view. I want a context menu to appear when i long press the list item within the dialog. The context menu appears but nothing happens when i click any of it's items. I've provided the actions to perform when a context menu item is clicked but nothing happens. Can anyone pls help?

     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();

Upvotes: 1

Views: 1463

Answers (1)

Руслан И.
Руслан И.

Reputation: 21

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

In summary just setOnMenuItemClickListener and @Override onMenuItemClick. Hope it help :)

Upvotes: 1

Related Questions