OnCreateContextMenu and ListView Elements

I have a LisView with several elements. I connected OnItemClickListener to this (as an inner class), for example:

lv.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    Toast.makeText(ShoppingListApp02Activity.this, "List item selected:" +  
    items.get(position).getId(), Toast.LENGTH_LONG).show();
    }
});

As you can see, when you select entriy, it displays the object elements of this record, in this example the object identifier is Item Item (not the list identifier, but the object identifier specified when creating the ArrayList elements). This works well and allows me to do whatever I want with the selected elements.

Now I also want to listen to the long-click listener, which will open the context menu for the selected ListView. How should I do it? I was able to connect the onCreateContextMenu listener to the ListView, but I do not see how I can get the ArrayList elements, how using onItemClickListener?

Here is what I have:

public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {  
    menu.add(0, v.getId(), 0, "Something");
    menu.add(0, v.getId(), 0, "Something else");  
}

OnCreateConextMenu , OnItemClickListener, ArrayList, , OnItemClickListener?

+5
6

, :

:

public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {  

    // Get the list
    ListView list = (ListView)v;

    // Get the list item position    
    AdapterContextMenuInfo info = (AdapterContextMenuInfo)menuInfo;
    int position = info.position

    // Now you can do whatever.. (Example, load different menus for different items)
    list.getItem(position);
    ...
}
+13

, ( - , ), ListView onItemLongClick , . :

lv.setOnItemLongClickListener(new OnItemLongClickListener() {
   @Override
   public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2,
       long arg3) {
        // TODO Auto-generated method stub

        return false;
   }
});

.

+6

.

convertView.setOnLongClickListener(new OnLongClickListener() {

            @Override
            public boolean onLongClick(View v) {
                ((Activity)mContext).openContextMenu(v);
                return true;
            }
        });

, , .

+5

, :

registerForContextMenu(YOUR LIST-VIEW OBJECT);

onCreateContextMenu():

public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {  
    menu.add(0, v.getId(), 0, "Something");
    menu.add(0, v.getId(), 0, "Something else");  
}

longClickListener ContextMenu.

, .

+4

, onContextItemSelected()

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


    menu.setHeaderTitle(title);

    menu.add(0, CMD_EDIT, 0, R.string.context_menu_edit);
    menu.add(0, CMD_DELETE, 0, R.string.context_menu_delete);


}

@Override
public boolean onContextItemSelected(MenuItem item) {

    switch (item.getItemId()) {
    case CMD_EDIT:
        any_function();//add your functionality here i.e. what you want to do
        return true;
    case CMD_DELETE:
        **confirmDelete**();
        return true;
    default:
        return super.onContextItemSelected(item);
    }
}

, ...

+1
source

Try this for the View element in recycleView

viewForContextMenu.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() {
        @Override
        public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
            menu.add("Context Menu Item1").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem item) {

                    //What should happent on selecting context menu item 1
                    return true;
                }
            });
        }
    });

You can use it with data customization in the ViewHolder element

0
source

All Articles