Listview long press the event and get the value of the selected row

Source: Internet
Author: User

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

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.