General steps of listview Programming
1) Declare the listview control in the layout File
2) use a one-dimensional or multi-dimensional dynamic array to save the data to be displayed in the listview;
3) Build an adapter and bind the data to the layout page of the displayed data;
4) set the adapter to listview through setadapter ()
Step 1: Write the layout file main. xml and add three textviews and listview to implement the overall layout. The Code is as follows:
View code
1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:orientation="vertical"
4 android:layout_width="fill_parent"
5 android:layout_height="fill_parent"
6 >
7 <TextView android:layout_width="fill_parent"
8 android:layout_height="wrap_content"
9 android:text="@string/tv1"
10 android:background="#FFF"
11 android:textColor="#888"
12 android:gravity="center"/>
13 <ListView android:id="@+id/lvCheckedTextView"
14 android:layout_width="fill_parent"
15 android:layout_height="wrap_content"/>
16
17 <TextView android:layout_width="fill_parent"
18 android:layout_height="wrap_content"
19 android:text="@string/tv2"
20 android:background="#FFF"
21 android:textColor="#888"
22 android:gravity="center"/>
23 <ListView android:id="@+id/lvRadioButton"
24 android:layout_width="fill_parent"
25 android:layout_height="wrap_content"/>
26
27 <TextView android:layout_width="fill_parent"
28 android:layout_height="wrap_content"
29 android:text="@string/tv3"
30 android:background="#FFF"
31 android:textColor="#888"
32 android:gravity="center"/>
33 <ListView android:id="@+id/lvCheckedButton"
34 android:layout_width="fill_parent"
35 android:layout_height="wrap_content"/>
36
37 </LinearLayout>
Step 2: modify the content of the string. xml file to facilitate access by the program or main. xml. The specific code is as follows:
View code
1 <? XML version = "1.0" encoding = "UTF-8"?>
2 <resources>
3 <string name = "hello"> Hello world, listview02activity! </String>
4 <string name = "app_name"> listview02 </string>
5 <string name = "TV1"> single check </string>
6 <string name = "TV2"> radiobutton </string>
7 <string name = "TV3"> checkbox </string>
8 </resources>
Step 3: Modify listview01.java and add operations related to listview. The Code is as follows:
View code
1 package cn.shaoyangjiang.com;
2
3 Import Android. App. activity;
4 Import Android. OS. Bundle;
5 import Android. widget. arrayadapter;
6 Import Android. widget. listview;
7
8 public class listview02activity extends activity {
9/** called when the activity is first created .*/
10 @ override
11 Public void oncreate (bundle savedinstancestate ){
12 Super. oncreate (savedinstancestate );
13 setcontentview (R. layout. Main );
14 // get the three listview
15 listview listview1 = (listview) findviewbyid (R. Id. lvcheckedtextview );
16 listview listview2 = (listview) findviewbyid (R. Id. lvradiobutton );
17 listview listview3 = (listview) findviewbyid (R. Id. lvcheckedbutton );
18 // use string to save the data to be displayed by listview
19 string [] DATA = new string []
20 {"yanyangjiang refueling", "you will succeed "};
21 // create an adapter and bind the data to the layout page of the displayed data;
22 arrayadapter <string> lv1adapter = new arrayadapter <string> (this, Android. R. layout. simple_list_item_checked, data );
23 // set the adapter to listview through setadapter ()
24 listview1.setadapter (lv1adapter );
25 // set the content in listview to single choice
26 listview1.setchoicemode (listview. choice_mode_single );
27
28 arrayadapter <string> lv2adapter = new arrayadapter <string> (this, Android. R. layout. simple_list_item_single_choice, data );
29 listview2.setadapter (lv2adapter );
30 listview2.setchoicemode (listview. choice_mode_single );
31
32 arrayadapter <string> lv3adapter = new arrayadapter <string> (this, Android. R. layout. simple_list_item_multiple_choice, data );
33 listview3.setadapter (lv3adapter );
34 listview3.setchoicemode (listview. choice_mode_multiple );
35}
36}
The specific effect is as follows:
If you want to learn more, the following links are good.
Android adapter usage Summary: http://kb.cnblogs.com/a/2328334/