Use of listview

Source: Internet
Author: User

AttachedCode

 

View code

 Package  Com. Yarin. Android. examples_04_04;  Import  Android. App. activity;  Import  Android. database. cursor;  Import  Android. Graphics. color;  Import  Android. OS. Bundle;  Import  Android. provider. contactscontract; Import  Android. provider. contactscontract. phonelookup;  Import  Android. View. view;  Import  Android. widget. adapterview;  Import  Android. widget. linearlayout;  Import  Android. widget. listadapter;  Import  Android. widget. listview;  Import  Android. widget. simplecursoradapter; Import  Android. widget. Toast;  Public   Class Activity01 Extends  Activity {linearlayout m_linearlayout; listview m_listview;  /**  Called when the activity is first created.  */  @ Override  Public   Void  Oncreate (bundle savedinstancestate ){  Super . Oncreate (savedinstancestate );  /*  Create a linearlayout layout object  */  M_linearlayout = New Linearlayout ( This  );  /*  Set the properties of the layout linearlayout  */  M_linearlayout.setorientation (linearlayout. Vertical); m_linearlayout.setbackgroundcolor (Android. Graphics. color. Black );  /* Create a listview object  */  M_listview = New Listview ( This  ); Linearlayout. layoutparams Param = New  Linearlayout. layoutparams (linearlayout. layoutparams. fill_parent, linearlayout. layoutparams. wrap_content); m_listview.setbackgroundcolor (color. Black );  /*  Add m_listview to m_linearlayout Layout  */ M_linearlayout.addview (m_listview, Param );  /*  Set the display m_linearlayout Layout  */  Setcontentview (m_linearlayout );  //  Obtain the database phones cursor Cursor cur = getcontentresolver (). Query (contactscontract. Contacts. content_uri, Null , Null , Null , Null  ); Startmanagingcursor (cur ); //  Listadapter is a bridge between listview and background data Listadapter adapter = New Simplecursoradapter ( This  ,  //  Define a display template for each row in the list  //  Indicates that each row contains two data items.  Android. R. layout. simple_list_item_2,  //  Database cursor object  Cur,  // Retrieve data from the name and number columns of the database  //  New String [] {phonelookup. display_name, phonelookup. Number}              New  String [] {phonelookup. display_name, phonelookup. _ id },  //  Views corresponding to name and number              New   Int  [] {Android. R. Id. text1, Android. R. Id. text2 });  /*  Add the adapter to m_listview  */ M_listview.setadapter (adapter );  /*  Add a setonitemselectedlistener listener to the m_listview View  */  M_listview.setonitemselectedlistener (  New  Adapterview. onitemselectedlistener () {@ override  Public   Void Onitemselected (adapterview <?> Arg0, view arg1, Int Arg2, Long  Arg3) {displaytoast ( "Scroll to" + long. tostring (arg0.getselecteditemid () + "item" ) ;}@ Override  Public   Void Onnothingselected (adapterview <?> Arg0 ){  //  Not selected  }});  /*  Add a setonitemclicklistener listener to the m_listview View  */  M_listview.setonitemclicklistener (  New Adapterview. onitemclicklistener () {@ override  Public   Void Onitemclick (adapterview <?> Arg0, view arg1, Int Arg2, Long  Arg3 ){  //  To process the selected items Displaytoast ("selected" + integer. tostring (arg2 + 1) + "item" );}});}  /*  Show toast  */     Public   Void  Displaytoast (string Str) {toast. maketext (  This  , STR, Toast. length_short). Show ();}} 

The commented-out code is incorrect. I used the ID code to call the phone number to facilitate testing.

The usage of listview, especially the setonitemselectedlistener and setonitemclicklistener events, may be confusing to some children's shoes.

setonitemclicklistener -- this event is triggered when a row in the list is clicked. Items, such as touch screen clicks and buttons.
setonitemselectedlistener: this parameter is triggered only after you click (trackball or confirmation key), and setonitemclicklistener triggers the setonitemselectedlistener after the selected object is changed.
to trigger an event by clicking a row in the list, we recommend that you use the setonitemclicklistener event.

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.