在Android应用中,ListView的使用绝对不会少见,其使用频率是很高的。
ListView是一个列表显示的控件,可以显示各种各样的内容,还可以为每条Item实现点击时间。哎呀,啰嗦了,其实如果大家能够看到这里说明大家都知道ListView的用处了。直接来讲LIstView的实现方法:
1,用数组保存要显示的数组;
2,建立适配器,放入每个Item要显示的内容,一般使用提供的SimpleAdapter;
3,把适配器放入到ListView中显示。
先看看本文例子的效果:
所以,首先在布局管理器放入一个ListView控件(其他为辅助类控件):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="135dp"
android:layout_height="match_parent"
android:text="课程名称" />
<TextView
android:layout_width="85dp"
android:layout_height="match_parent"
android:text="学分" />
<TextView
android:layout_width="40dp"
android:layout_height="match_parent"
android:text="成绩" />
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#000000" />
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="350dp"
android:layout_weight="0.61" />
</LinearLayout>
接入Listview后,还应该设置ListView内Item里面的布局,所以新建布局文件user.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/name"
android:layout_width="144dp"
android:layout_height="40dp" />
<TextView
android:id="@+id/credit"
android:layout_width="83dp"
android:layout_height="40dp" />
<TextView
android:id="@+id/mark"
android:layout_width="match_parent"
android:layout_height="40dp" />
</LinearLayout>
接下来就转战java文件了,代码都有注释了,如下
public class MainActivity extends ListActivity {
// 准备要显示的数据
private String data[][] = { { "线性代数", "2", "85" }, { "高等数学", "4", "80" },
{ "C语言编程", "2", "87" }, { "信号与系统", "4", "94" },
{ "数字信号处理", "2", "78" }, { "高频电子线路", "4", "68" },
{ "通信原理", "3", "89" }, { "移动通信原理", "2", "88" },
{ "光纤通信原理", "3", "68" }, { "微波通信原理", "3", "78" },
{ "java面向对象基础", "2", "76" }, { "电磁场与电磁波", "3", "85" },
{ "微机原理", "2", "86" }, { "单片机应用设计", "1", "73" } };
private ListView listview = null; // 定义ListView组件
private List<Map<String, Object>> datalist = new ArrayList<Map<String, Object>>(); // 定义用于数据显示的包装
private SimpleAdapter simpleAdapter = null; // 适配器
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listview = (ListView) findViewById(android.R.id.list);//取得组件
for (int i = 0; i < data.length; i++) {
Map<String, Object> map = new HashMap<String, Object>();//map集合,放入的是每个Item要显示的数据
//放入数据
map.put("name", data[i][0]);
map.put("credit", data[i][1]);
map.put("mark", data[i][2]);
datalist.add(map);
}
SimpleAdapter listAdapter = new SimpleAdapter(this, datalist,
R.layout.user, new String[] { "name", "credit", "mark" },//数据的键值对的key
new int[] { R.id.name, R.id.credit, R.id.mark }); //Item布局文件的id
setListAdapter(listAdapter); //添加适配器
}
protected void onListItemClick(ListView l, View v, int position, long id) { //定义Item点击事件,本例没有进行说明
super.onListItemClick(l, v, position, id);
System.out.println("id------->" + id);
System.out.println("position--------->" + position);
}
}
这样,工作就完成了,可以完成ListView对数据的显示,因为相对简单,就不多写了吧。
关于对ListView的事件处理以及和数据库配合显示查询等一些使用方法,以后会有提到和做一些相关的学习记录。
晚安。。。。。。。。。