android Listview2 笔记
ListView编程的一般步骤
1)在布局文件中声明ListView控件
2) 使用一维或多维动态数组保存ListView要显示的数据 ;
3) 构建适配器Adapter,将数据与显示数据的布局页面绑定;
4)通过setAdapter()方法把适配器设置给ListView
第一步:编写布局文件main.xml,添加三个Textview和listview实现整体布局。具体代码如下

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访问,具体代码如下

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的相关操作,具体代码如下

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/
标签:
android listview
posted on 2012-02-24 11:54 forrest001 阅读(1199) 评论(0) 编辑 收藏 举报
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架