Android learning ---------- long press list items pop-up menu, add events to menu items, get Context

Source: Internet
Author: User

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 ();}}


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.