Arrayadapter Introduction and the use of the ListView:
Adapter is a bridge between data and views, and the data is processed in Adapter and then displayed on top of the view. is used to bind the data.
Use a small example to introduce:
public class Friendactivity extends Activity {private ListView listview; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.main);//We set up a list collection, Then add several data to the inside list<string> ls = new arraylist<string> (), Ls.add ("Zhang San"); Ls.add ("John Doe"); Ls.add ("Harry");// Gets the XML file in the ListView control in ListView = (ListView) Findviewbyid (r.id.ayyay_list_list);//Then calls the Setadapter method for the ListView control, Let the data appear on the interface. Listview.setadapter (New arrayadapter<string> (this,android. R.layout.simple_list_item_1,ls));}
Use the steps.
( 1 ) define an array to hold the listview
( 2 ) by implementing arrayadapter constructor to create a
(3) bind arrayadapterthrough the Setadapter () method of the ListView .
In the second step, it is necessary to say thatArrayadapter has multiple constructors, the most common one implemented in the example.
The first parameter is a context object, which can be replaced directly with this.
The second parameter is a layout resource ID that contains TextView, which is used to populate each row of the ListView (in the code above: android.r.layout.simple_list_item_ 1 is the system comes with the format, directly take to call it).
A third parameter is ListView the content.
Second, monitor the ListView object:
There are three commonly used to monitor a ListView object:
1.OnItemClickListener: Listener triggers when clicking on a child of this item
2.OnTouchListener: Listener triggers when touching the ListView
3.OnScrollListener: Listener triggers when the ListView Scrolls
(1), Onitemclicklistener
public class Friendactivity extends Activity {private ListView listview; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.main);//We set up a list collection, Then add several data to the inside list<string> ls = new arraylist<string> (), Ls.add ("Zhang San"); Ls.add ("John Doe"); Ls.add ("Harry");// Gets the XML file in the ListView control in ListView = (ListView) Findviewbyid (r.id.ayyay_list_list);//Then calls the Setadapter method for the ListView control, Let the data appear on the interface. Listview.setadapter (New arrayadapter<string> (this,android. R.LAYOUT.SIMPLE_LIST_ITEM_1,LS);//Monitor the ListView object: Triggers Listview.setonitemclicklistener when the sub-item is clicked (new Itemclickevent ());} Inherit Onitemclicklistener, trigger private final class Itemclickevent implements onitemclicklistener{@Override when sub-item is clicked It is important to note that the third parameter arg2, which is represented by clicking the number of options public void Onitemclick (adapterview<?> arg0, View arg1, int arg2,long arg3) {// Click the event to get the contents of the click option string text = Listview.getitematposition (arg2) + "";//show it through the spinning object. Toast.maketext (Getapplicationcontext (), text, 1). Show ();}}
As you can see, when you click the second item John Doe, the bottom will show a John Doe with the spin object
Write here first, after the lesson back to write = =
Android Development Series (14): ListView usage, three ways to monitor ListView, and how to use adapter in a ListView