android Listview2 笔记
ListView编程的一般步骤
1)在布局文件中声明ListView控件
2) 使用一维或多维动态数组保存ListView要显示的数据 ;
3) 构建适配器Adapter,将数据与显示数据的布局页面绑定;
4)通过setAdapter()方法把适配器设置给ListView
第一步:编写布局文件main.xml,添加三个Textview和listview实现整体布局。具体代码如下
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>
第二步:修改String.xml文件的内容,方便程序或者main.xml访问,具体代码如下
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">单项打勾</string>
6 <string name="tv2">RadioButton</string>
7 <string name="tv3">CheckBox</string>
8 </resources>
第三步:修改ListView01.java,添加listview的相关操作,具体代码如下
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 //得到三个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 //用string来保存listview要显示的数据
19 String[] data = new String[]
20 {"邵洋江加油","你会成功的"};
21 //构建适配器Adapter,将数据与显示数据的布局页面绑定;
22 ArrayAdapter<String> lv1Adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_checked,data);
23 //通过setAdapter()方法把适配器设置给ListView
24 listview1.setAdapter(lv1Adapter);
25 //listview里的内容设置为单选
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 }
具体效果如下:
如果还想深入了解,下面的链接不错
Android之Adapter用法总结:http://kb.cnblogs.com/a/2328334/
posted on 2012-02-24 11:54 forrest001 阅读(1199) 评论(0) 编辑 收藏 举报