Reprinted: http://houwanji.blog.163.com/blog/static/1622762200972644732128/
First, add the registerforcontextmenu (getlistview (); statement at the end of the oncreate function,
This means that every item of the listview in the screen is regardedMenu
To register,
Then inherit the oncreatecontextmenu () method as follows:
@ Override //Create
Long pressPop-up
Menu
This is a callback function.
Public void oncreatecontextmenu (contextmenu menu, view V, contextmenuinfo menuinfo ){
Super. oncreatecontextmenu (menu, V, menuinfo );
Menu. Add (0, delete_id, 0, R. String. menu_delete); // Add twoMenu
Item
Menu. Add (0, insert_id, 0, R. String. menu_insert );
}
Finally, SetMenu
Action after the item is selected
@ Override
Public Boolean oncontextitemselected (menuitem item ){
Switch (item. getitemid ()){
Case delete_id:
Adaptercontextmenuinfo info = (adaptercontextmenuinfo) item. getmenuinfo (); // getMenu
Information
Mdbhelper. deletenote (info. ID); // Delete selected note
Filldata ();
Return true;
}
Return super. oncontextitemselected (item );
}
You can use the setonitemlongclick function of listview to associate a long press event. However, in the onitemlongclick function of the response, you cannot call the listview display function.Menu
.
InAndroid
OfMenu
There are two types: Options menu and context menu. (There are also three types: Options Menu
, Context menu, sub menu) where options menu is associated with activity, context menu
Is associated with the view. We need to clickMenu
Belongs to the context menu, but the context menu
Only setoncreatecontextmenulistener can be used for association.
To achieve this effect, you need to understand what happens after the listview item is long pressed:
The onitemlongclick function is triggered first, and the onitemlongclick operation is executed. Then, the onitemlongclick function is returned.
Whether the oncreatecontextmenulistener function is executed. If onitemlongclick returns false, the system continues to execute
Oncreatecontextmenulistener function. Otherwise, true is returned. The oncreatecontextmenulistener function is ignored.
Therefore, we can determine the onitemlongclick and prepare the data needed in oncreatecontextmenulistener, and then call the data display in oncreatecontextmenulistener.