Here is a ListView first, long press a list item in the ListView, pop Up a menu, the menu has two entries, "Update this bar" and "Delete this Bar", and both of them have listening events, the whole is like a friend dialogue operation. Where the menu item has an action to get context: adaptercontextmenuinfo info = (adaptercontextmenuinfo) item.getmenuinfo (), which is also used for list items.
The specific code is as follows:
protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_second); show = (ListView) Findviewbyid (r.id.show);d B = Sqlitedatabase.openorcreatedatabase ( This.getfilesdir (). toString () + "/MUSIC.DB3", null);//Display list showlist ();// Add long Press Click to pop up select Menu Show.setoncreatecontextmenulistener (New Oncreatecontextmenulistener () {public void Oncreatecontextmenu (ContextMenu menu, View v,contextmenuinfo menuinfo) {menu.setheadertitle ("select operation"), Menu.add (0, 0, 0, "update this article"), Menu.add (0, 1, 0, "delete the article");});} Add event to menu item @overridepublic Boolean oncontextitemselected (MenuItem item) {Adaptercontextmenuinfo info = ( Adaptercontextmenuinfo) Item.getmenuinfo ();//info.id gets the idstring id = string.valueof (info.id) of the entry bindings selected in the ListView. Switch (Item.getitemid ()) {case 0:updatedialog (ID); Method of updating Events return True;case 1://system.out.println ("delete" +info.id);d Eletedata (Db,id); Delete Event method Showlist (); return True;default:return super.oncontextitemselected (item);}}
Show list item private void Showlist () {cursor cursor = Null;try{cursor = Db.rawquery ("SELECT * from Musiclist", null); inflatelist (cursor);} catch (Sqliteexception se) {db.execsql ("CREATE Table musiclist (_id integer primary key autoincrement," + "music varchar (30 ), "+" singer varchar (30));//Query cursor = db.rawquery ("SELECT * from Musiclist", null); inflatelist (cursor);} Finally{//cursor.close ();}}