本文实例讲述了android编程使用listview实现数据列表显示的方法。分享给大家供大家参考,具体如下:
要将数据库中的数据列表显示在屏幕上,我们要使用listview这个控件,当用户从数据库中取出数据时,要将数据绑定到显示控件上,如何绑定呢,我们需要创建适配器进行绑定,创建适配器有两种方式:
第一种是用simpleadapter创建(要求绑定的数据是list<hashmap<string, object>>数据类型)
第二种是用simplecursoradapter创建(要求绑定的数据是cursor数据类型)
显示效果如图所示:
界面布局:
item.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
<?xml version= "1.0" encoding= "utf-8" ?>
<!--item -->
<linearlayout
xmlns:android= "http://schemas.android.com/apk/res/android"
android:orientation= "horizontal"
android:layout_width= "fill_parent"
android:layout_height= "fill_parent" >
<!-- 名称 -->
<textview
android:layout_width= "130dp"
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>
|
main.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
<?xml version= "1.0" encoding= "utf-8" ?>
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"
android:orientation= "vertical"
android:layout_width= "fill_parent"
android:layout_height= "fill_parent"
>
<!-- 标题 -->
<linearlayout
android:orientation= "horizontal"
android:layout_width= "fill_parent"
android:layout_height= "wrap_content" >
<textview
android:layout_width= "130dp"
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控件 -->
<listview
android:layout_width= "fill_parent"
android:layout_height= "fill_parent"
android:id= "@+id/listview"
/>
</linearlayout>
|
使用simpleadapter进行数据绑定
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
public class mainactivity extends activity {
private personservice service;
@override
public void oncreate(bundle savedinstancestate) {
super .oncreate(savedinstancestate);
setcontentview(r.layout.main);
service = new personservice( this );
listview listview = (listview) this .findviewbyid(r.id.listview);
//获取到集合数据
list<person> persons = service.getscrolldata( 0 , 10 );
list<hashmap<string, object>> data = new arraylist<hashmap<string,object>>();
for (person person : persons){
hashmap<string, object> item = new hashmap<string, object>();
item.put( "id" , person.getid());
item.put( "name" , person.getname());
item.put( "phone" , person.getphone());
item.put( "amount" , person.getamount());
data.add(item);
}
//创建simpleadapter适配器将数据绑定到item显示控件上
simpleadapter adapter = new simpleadapter( this , data, r.layout.item,
new string[]{ "name" , "phone" , "amount" }, new int []{r.id.name, r.id.phone, r.id.amount});
//实现列表的显示
listview.setadapter(adapter);
//条目点击事件
listview.setonitemclicklistener( new itemclicklistener());
}
//获取点击事件
private final class itemclicklistener implements onitemclicklistener{
public void onitemclick(adapterview<?> parent, view view, int position, long id) {
listview listview = (listview) parent;
hashmap<string, object> data = (hashmap<string, object>) listview.getitematposition(position);
string personid = data.get( "id" ).tostring();
toast.maketext(getapplicationcontext(), personid, 1 ).show();
}
}
}
|
使用simplecursoradapter进行数据绑定
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
public class mainactivity extends activity {
private personservice service;
@override
public void oncreate(bundle savedinstancestate) {
super .oncreate(savedinstancestate);
setcontentview(r.layout.main);
service = new personservice( this );
listview listview = (listview) this .findviewbyid(r.id.listview);
//获取游标
cursor cursor = service.getcursorscrolldata( 0 , 10 );
//创建simplecursoradapter适配器将数据绑定到item显示控件上
simplecursoradapter adapter = new simplecursoradapter( this , r.layout.item, cursor,
new string[]{ "name" , "phone" , "amount" }, new int []{r.id.name, r.id.phone, r.id.amount});
listview.setadapter(adapter);
//条目点击事件
listview.setonitemclicklistener( new itemclicklistener());
}
private final class itemclicklistener implements onitemclicklistener{
public void onitemclick(adapterview<?> parent, view view, int position, long id) {
listview listview = (listview) parent;
cursor cursor = (cursor) listview.getitematposition(position);
string personid = string.valueof(cursor.getint(cursor.getcolumnindex( "_id" )));
toast.maketext(getapplicationcontext(), personid, 1 ).show();
}
}
}
|
注意:使用第二种方式在获取数据集合时必须指定主键"_id"
希望本文所述对大家android程序设计有所帮助。