Android Learning----------Long Press the list item pop-up menu, add event to menu item, get context

Source: Internet
Author: User

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


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.