Android Development Series (14): ListView usage, ListView listening methods, and the use of Adapter in ListView

Source: Internet
Author: User

Android Development Series (14): ListView usage, ListView listening methods, and the use of Adapter in ListView

I. Introduction of ArrayAdapter and usage of ListView:

The Adapter is a bridge between data and views. data is processed in the adapter and displayed on The View. It is used to bind data.

Here is a small example:

 

Public class FriendActivity extends Activity {private ListView listView; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); // set a List set and add several data lists to it.
 
  
Ls = new ArrayList
  
   
(); Ls. add (Michael); ls. add (Li Si); ls. add (Wang Wu); // obtain the listView control listView = (ListView) findViewById (R. id. ayyay_list_list); // call the setAdapter method for the listView control to display the data on the interface. ListView. setAdapter (new ArrayAdapter
   
    
(This, android. R. layout. simple_list_item_1, ls ));}
   
  
 

 

Procedure.

(1) define an array to store the items in the ListView (such as the ls set in the above Code ).

(2) create an ArrayAdapter object by implementing the ArrayAdapter constructor.

(3) bind ArrayAdapter through the setAdapter () method of ListView.

In step 2, it is necessary to mention that ArrayAdapter has multiple constructor functions. In this example, the most commonly used constructor is implemented.

The first parameter is the context object, which can be replaced by this directly.

The second parameter is a layout resource ID containing TextView, which is used to fill each row of ListView (in the above Code: android. r. layout. simple_list_item_1 is a built-in system format, which is called directly ).

The third parameter is the content of ListView.


2. Listening to ListView objects:

There are three commonly used ListView object listeners:

1. OnItemClickListener: triggered when the listener clicks a subitem of the project.
2. OnTouchListener: triggered when the listener touches the ListView.
3. OnScrollListener: Listener triggered when ListView is rolled

 

(1) OnItemClickListener

 

Public class FriendActivity extends Activity {private ListView listView; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); // set a List set and add several data lists to it.
 
  
Ls = new ArrayList
  
   
(); Ls. add (Michael); ls. add (Li Si); ls. add (Wang Wu); // obtain the listView control listView = (ListView) findViewById (R. id. ayyay_list_list); // call the setAdapter method for the listView control to display the data on the interface. ListView. setAdapter (new ArrayAdapter
   
    
(This, android. r. layout. simple_list_item_1, ls); // listens to the listView object: triggers the listView when the subitem is clicked. setOnItemClickListener (new ItemClickEvent ();} // inherits OnItemClickListener, when a subitem is clicked, the private final class ItemClickEvent implements OnItemClickListener {@ Override // The third parameter arg2 is triggered. This parameter indicates the public void onItemClick (AdapterView)
    Arg0, View arg1, int arg2, long arg3) {// click the event to obtain the content of the click option String text = listView. getItemAtPosition (arg2) +; // display the data through a silent object. Toast. makeText (getApplicationContext (), text, 1). show ();}}}
   
  
 

As you can see, when you click the second item Li Si, A Li Si object will be displayed below

 

 

 

 

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.