I learned android as an amateur interest and made a small application while learning it. The listview long press event was used yesterday. A drop-down menu is displayed, which serves as a knowledge point for future study.
First, assume that the listview already has data, whether it is obtained from SQLite orProgramThe fixed value. The following describes how to write a long-based event.
In Activiy:
// Listview lvjzw; // listview display list
Lvjzw. setoncreatecontextmenulistener (lvjzwoncreatecontextmenulistener );
/**
* Long press: a drop-down menu is displayed. A drop-down box is displayed in this event.
*/
Oncreatecontextmenulistener lvjzwoncreatecontextmenulistener = new oncreatecontextmenulistener ()
{
@ Override
Public void oncreatecontextmenu (contextmenu menu, view V, contextmenuinfo menuinfo)
{
Menu. setheadertitle ("share to contact"); // Title
Menu. Add (0, 1, 0, "Send to friend via SMS"); // drop-down menu
// Menu. Add (0, 2, 0, "pop-up menu 2 ");
}
};
/**
* Long-Press the menu Response Function and obtain the content of the selected listview row
*/
@ Override
Public Boolean oncontextitemselected (menuitem item)
{
Contextmenuinfo info = item. getmenuinfo ();
Adapterview. adaptercontextmenuinfo contextmenuinfo = (adaptercontextmenuinfo) Info;
// Obtain the position of the selected row
Int position = contextmenuinfo. position;
// Obtain the question content
Question = datarecordlist. Get (position). getquestion ();
Toast. maketext (jzwactivity. This, question + "=", Toast. length_short). Show ();
/********************************* So far, you can obtain the value *********************************** in the long-pressed column listview ****************************** **************************************** *******************/
// Call the mobile phone contact to send the text message
Intent intent = new intent ();
Intent. setaction (intent. action_pick );
Intent. setdata (contacts. Phones. content_uri );
Startactivityforresult (intent, pick_contact_subactivity );
Return super. oncontextitemselected (item );
}
Long press a row. The drop-down menu is displayed,
Click to display the selected content in toast