A listview is displayed first, and a list item of the listview is long pressed. A menu is displayed. There are two entries in the menu: "update this entry" and "Delete this entry ", both of these two have listening events, which are the same as friend conversation operations. The menu item has an operation to get the context: AdapterContextMenuInfo info = (AdapterContextMenuInfo) item. getMenuInfo (); this statement is also used for list items.
The Code is as follows:
Protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_second); show = (ListView) findViewById (R. id. show); db = SQLiteDatabase. openOrCreateDatabase (this. getFilesDir (). toString () + "/music. db3 ", null); // display the list showlist (); // Add a long press and click it to bring up the show menu. setOnCreateContextMenuListener (new OnCreateContextMenuListener () {public void onCreateContextMenu (ContextMenu menu, View v, ContextMenuInfo menuInfo) {menu. setHeaderTitle ("Select Operation"); menu. add (0, 0, 0, "update this entry"); menu. add (0, 1, 0, "Delete this entry") ;}}}// add an event to the menu item @ Overridepublic boolean onContextItemSelected (MenuItem item) {AdapterContextMenuInfo info = (AdapterContextMenuInfo) item. getMenuInfo (); // info. id to obtain the idString id = String. valueOf (info. id); switch (item. getItemId () {case 0: updateDialog (id); // Method for updating events return true; case 1: // System. out. println ("delete" + info. id); deleteData (db, id); // The showlist (); return true; default: return super. onContextItemSelected (item );}}
// Display the list item private void showlist () {Cursor cursor = null; try {cursor = db. rawQuery ("select * from musiclist", null); inflateList (cursor);} catch (SQLiteException semo-mongodb.exe cSQL ("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 ();}}