There are several options in the ListView, which can pop up a context menu and get detailed information about the options that you press by long pressing each option.
Key steps:
private static final int delete_id = Menu.first + 1;// uses Menu.first constants instead of other constants: program-encapsulated variables are not used to account for memory program read faster error- prone
private static final int Edit_id=menu.first + 2;
private static final int View_id=menu.first + 3;
1. Register the context menu for the ListView in Method OnCreate (as shown in the Scarlet Letter)
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (r.layout.diary_list);
Mdbhelper = new Diarydbadapter (this);
Mdbhelper.open ();
Renderlistview ();
Registerforcontextmenu (Getlistview ());//Create a context menu for each item
}
2, @Override//create long press the popup menu, which is a callback function
Public void oncreatecontextmenu(ContextMenu menu, View V,
contextmenuinfo Menuinfo) {
//TODO auto-generated method stub
Super.oncreatecontextmenu (menu, V, menuinfo);
//Add menu item
menu.add (0, delete_id, 1, r.string.menu_delete);
menu.add (0, edit_id, 2, r.string.menu_edit);
menu.add (0, view_id, 3, r.string.menu_view);
}
Responses to items in the menu:
public Booleanoncontextitemselected(MenuItem Item) {
adaptercontextmenuinfo info = (adaptercontextmenuinfo) item.getmenuinfo ();
long id=info.id;//key for current item, unique
int position=info.position;//The position of the current item in the ListView
Cursor c = mdiarycursor;
c.movetoposition (position);
Selected_id=id;
selected_title=c.getstring (C.getcolumnindex (Diarydbadapter.key_title));
selected_body=c.getstring (C.getcolumnindex (diarydbadapter.key_body));
selected_created=c.getstring (C.getcolumnindex (diarydbadapter.key_created));
switch (Item.getitemid ()) {
Case DELETE_ID:
Mdbhelper.deletediary (info.id);
Renderlistview ();
return true;
Case EDIT_ID:
intent i = new Intent (Activitymain.this, Activitydiaryedit.class);
i.putextra (Diarydbadapter.key_rowid, SELECTED_ID);
i.putextra (Diarydbadapter.key_title, Selected_title);
i.putextra (Diarydbadapter.key_body, Selected_body);//returns the specified column name from zero, and throws a IllegalArgumentException exception if it does not exist.
startactivityforresult (i, Activity_edit);
return true;
case view_id:
alertdialog.builder builder=new Alertdialog.builder (activitymain.this);
Builder.settitle (selected_title+ "(" +selected_created+ ")");
builder.setmessage (selected_body);
Builder.setpositivebutton ("OK", new Dialoginterface.onclicklistener () {
@Override
Public void OnClick (dialoginterface dialog, int which) {
//TODO auto-generated method stub
Dialog.dismiss ();
}
});
builder.show ();
return true;
}
return super.oncontextitemselected (item);
}
or (turn from http://wen66.iteye.com/blog/1208617)
Private ListView fileList;
- Filelist.setoncreatecontextmenulistener (new Oncreatecontextmenulistener () {
- @Override
- public void Oncreatecontextmenu (ContextMenu menu, View v,contextmenuinfo info) {
- //TODO auto-generated method stub
- Menu.setheadertitle (R.string.contentmenu);
- Menu.add (0, Andboxconstant.open, 0, R.string.open);
- Menu.add (0, Andboxconstant.propertiy, 6, r.string.properties);
- }
- });
- @Override
- Public Boolean oncontextitemselected (MenuItem item) {
- switch (Item.getitemid ()) {
- Case Andboxconstant.open:
- .........
- Break ;
- Case andboxconstant.propertiy:
- .........
- Break ;
- Default:
- Break ;
- }
- return super.oncontextitemselected (item);
- }
- Filelist.setonitemlongclicklistener (new Onitemlongclicklistener () {
- @Override
- Public Boolean Onitemlongclick (adapterview<?> Ada, view view, int index, long longindex) { /c3>
- Filelist.showcontextmenu ();
- return true;
- }
- });
In processing long and on time, the attention to detail is to set Onitemlongclick return to True, otherwise long press will execute setonitemclicklistener.
=============================================================
Context Menu The context menu for Android is conceptually similar to the right-click menu for PC software. When a view is registered to a context menu, a "long press" (holding the hold for almost two seconds) on the object will appear with a floating menu that provides the relevant functionality. The context menu can be registered to any view object, however, most commonly used for List view listview item, when a list item is pressed, its background color is converted and the context menu is prompted to be rendered. (The Phone contact list provides a good example of this feature.) Note: The context menu item does not support icons or shortcut keys. In order to create a context menu, you must override the context menu callback function for this activity: Oncreatecontextmenu () and oncontextitemselected (). In the callback function Oncreatecontextmenu (), you can add a menu item by using an Add () method, or by augmenting a menu resource defined in XML. Then, register a context menu ContextMenu with Registerforcontextmenu () for this view.
For example, the following code can be used in a Notepad application to add a context menu for each comment in the list:
Java code
- Public void Oncreatecontextmenu (ContextMenu menu, View V,
- Contextmenuinfo menuinfo) {
- Super.oncreatecontextmenu (menu, V, menuinfo);
- Menu.add (0, edit_id, 0, "EDIT");
- Menu.add (0, delete_id, 0, "DELETE");
- }
- Public Boolean oncontextitemselected (MenuItem item) {
- Adaptercontextmenuinfo info = (adaptercontextmenuinfo) item.getmenuinfo ();
- switch (Item.getitemid ()) {
- Case edit_id:
- Editnote (info.id);
- return true;
- Case delete_id:
- Deletenote (info.id);
- return true;
- Default:
- return super.oncontextitemselected (item);
- }
- }
public void Oncreatecontextmenu (ContextMenu menu, View V, contextmenuinfo menuinfo) { Super.oncreatecontextmenu (menu, V, menuinfo); Menu.add (0, edit_id, 0, "EDIT"); Menu.add (0, delete_id, 0, "DELETE"); public boolean oncontextitemselected (MenuItem item) { Adaptercontextmenuinfo info = (adaptercontextmenuinfo) Item.getmenuinfo (); Switch (Item.getitemid ()) {case edit_id: editnote (info.id); return true; Case DELETE_ID: deletenote (info.id); return true; Default: return super.oncontextitemselected (item); } }
In Oncreatecontextmenu (), in addition to the ContextMenu that will add MenuItems, you need to give the selected view and a context menu information Contextmenuinfo object that provides additional information about the selected object. In this case, Oncreatecontextmenu () didn't do anything special-just added some menu items.
In the oncontextitemselected () callback function, we request adaptercontextmenuinfo from MenuItem, which provides information about the currently selected item. All we want from this is the list ID of the selected item, so whether we edit or delete a comment, we find the ID through the Adaptercontextmenuinfo.info field of the object. This ID is passed to the Editnote () and Deletenote () methods to perform the corresponding action.
now, to register the context menu for all items in a list view, we can pass the entire list view object to the Registerforcontextmenu (view) method:
Java code
- Registerforcontextmenu (Getlistview ());
Registerforcontextmenu (Getlistview ());
remember, You can pass any view object to register a context menu. Here, Getlistview () returns the List view object in the list activity listactivity that is used for the Notepad application. In this way, any item in this list is registered to this context menu.