您的位置:首页 > 移动开发 > Android开发

android学习笔记(7)ListView的使用

2013-11-21 10:10 429 查看

ListView的使用

ListView是一个常用的组件,它以列表形式显示内容,并且能根据数据的长度自适应显示。它的显示需要三个元素:
1.Item的xml文件
2.要绑定的数据
3.适配器来绑定数据与Item

完成的显示是这样的



1.首先编写Item的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:layout_width="120dp"
android:layout_height="wrap_content"
android:id="@+id/name"
/>

<TextView
android:layout_width="150dp"
android:layout_height="wrap_content"
android:id="@+id/phone"
/>

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/amount"
/>

</LinearLayout>


比较简单不做过多解释

然后是mainxml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="120dp"
android:layout_height="wrap_content"
android:text="姓名"
/>

<TextView
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="电话"
/>

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="余额"
/>

</LinearLayout>

<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listview"
/>

</LinearLayout>


2.取得要绑定在Item上面的数据
这里我们要显示的数据是从数据库中取出来的,数据库很简单如下



这里我们准备用SimpleAdapter来绑定数据与xml文件,SimpleAdapter支持的数据类型是List<HashMap<String,Object>> 所以我们先将数据转成该形式
List<Person> persons = service.getScrollData(0, 10);
List<HashMap<String,Object>> data = new ArrayList<HashMap<String,Object>>();
for(Person person:persons){
HashMap<String,Object> map = new HashMap<String,Object>();
map.put("name", person.getName());
map.put("phone", person.getPhone());
map.put("amount", person.getAmount());
map.put("id", person.getId());
data.add(map);
}


第一行代码是自己以前写的数据库的辅助类得方法,目的是取得数据库中的数据
然后是创建适配器,显示listview 如下
SimpleAdapter adapter = new SimpleAdapter(this,data,R.layout.first,
new String[]{"name","phone","amount"},new int[]{R.id.name,R.id.phone,R.id.amount});
listview.setAdapter(adapter);
这里对SimpleAdapter的构造器参数进行说明 param1 是上下文对象,param2 是要绑定的数据,param3 是要绑定的Item的xml文件,param4 与 param5 是将结果集中字段名为哪个的值 绑定到哪个显示控件中 如上面代码,
将结果集中字段名为name的值绑定到id为name的显示控件中。
然后将适配器设置到listview中

另外对SimpleCursorAdapter适配器进行简单说明,它的构造方法与SimpleAdapter类似 , param2是要绑定的xml文件 ,param3 是要绑定的数据的游标,其他参数一样。但是
注意:使用SimpleCursorAdapter适配器的时候,Cursor必须包含一个名称为 "_id" 的字段,否则它会不工作。这里有两种解决方法 (1) 修改数据库的表结构,加上"_id"
【一般不使用】 (2)Cursor执行SQL语句的时候对其中一个字段使用别名为"_id" 如 "select personid as _id,name,phone,amount from person order by personid asc limit ?,?"

3.对于ListView的setAdapter()方法简单说明
setAdapter方法内部会调用
int total = Adapter的getCount()方法,用来得到记录总数

然后根据条目的高度得到窗口上要显示多少个条目 int page = 10(假如计算出10个条目)
然后进行迭代 for(int i = 0;i<page;i++){
根据getView方法得到条目的界面,然后显示出来
View view = adapter.getView(i,convertView,parent)
}

最后附上MainActivity的代码
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
service = new PersonService(this);
listview = (ListView)this.findViewById(R.id.listview);
listview.setOnItemClickListener(new ItemClickListener());
show();
}

private vo
4000
id show() {
List<Person> persons = service.getScrollData(0, 10);
List<HashMap<String,Object>> data = new ArrayList<HashMap<String,Object>>();
for(Person person:persons){
HashMap<String,Object> map = new HashMap<String,Object>();
map.put("name", person.getName());
map.put("phone", person.getPhone());
map.put("amount", person.getAmount());
map.put("id", person.getId());
data.add(map);
}
//把数据绑定到条目上的某些显示控件
SimpleAdapter adapter = new SimpleAdapter(this,data,R.layout.first, new String[]{"name","phone","amount"},new int[]{R.id.name,R.id.phone,R.id.amount}); listview.setAdapter(adapter);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: