欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android ListView数据绑定显示的三种解决方法

程序员文章站 2023-12-11 23:04:28
首先,创建一个用于显示一个item的layout,名为item.xml复制代码 代码如下:

首先,创建一个用于显示一个item的layout,名为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:id="@+id/name"
android:layout_width="120dp"
android:layout_height="wrap_content" />

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

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

</linearlayout>


然后,在main.xml中,添加一个listview数据显示控件,添加id名称为listview

接下来便是进行数据的绑定,总共分为三种方法:

复制代码 代码如下:

 private void show1() {
  list persons = ps.getscolldata(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("amount", person.getamount());
   item.put("id", person.getid());
   item.put("name", person.getname());
   item.put("phone", person.getphone());
   data.add(item);
  }
  simpleadapter adapter = new simpleadapter(getapplicationcontext(),data, r.layout.item,
    new string[] { "name", "phone", "amount" }, new int[] {r.id.name, r.id.phone, r.id.amount });
          // item表示为之前定义的item.xml,表示将data集合中的每一个对象绑定到些item中,即将data中的每一项绑定到一个视图item上;
          // 后两个参数表示把结果集中哪些key的值绑定到哪些控件上;(把结果集中key为name对应的对象绑定到视图中id为name的控件上)
  listview.setadapter(adapter);//内部处理流程如下
  // {int total = adapter.getcount();// 获取得到的数据总数
  // int perpage = 7;//获取每一页的显示条目数,
  // for (int i = 0; i < perpage; i++) {
  // view view = adapter.getview(i, convertview, parent);//第二次执行时会将前一次执行的view传给convertview;
  // 显示条目
  // }}
 }

 private void show2() {//此方法需要一个结果集中的cursor,要求cursor中需要有一个名称为_id的字段;所以添加一个获取cursor的方法如下!
  cursor cursor = ps.getcursorscolldata(0, 10);
  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);
 }

     public cursor getcursorscolldata(int offest, int maxresult) {
  sqlitedatabase db = dbopenhelper.getreadabledatabase();//因为要求结果集中要有一个名为_id的字,所以sql语句如下!
  cursor cursor = db.rawquery("select personid as _id,name,phone,amount from person order by personid asc limit ?,?",
      new string[] { string.valueof(offest),string.valueof(maxresult) });
  // db.query(table, columns, selection, selectionargs, groupby, having,orderby, limit);
  return cursor;
 }

 private void show3() {
  list persons = ps.getscolldata(0, 10);// personadapter见下一章节自定义适配器
  personadapter adapter = new personadapter(getapplicationcontext(),persons, r.layout.item);
  listview.setadapter(adapter);
 }


当每点击一项需要获取此项的相关信息时可以添加此方法:
listview.setonitemclicklistener(new itemclicklistener());

 private final class itemclicklistener implements onitemclicklistener {
  @override//此方法中第一个参数为显示数据(item项)的控件,在此例子中即为listview;第三个参数为点击项的位置,
  public void onitemclick(adapterview<?> parent, view arg1, int position,long arg3) {
   listview lview = (listview) parent;
   // show3()方法对应的处理方法
   // person person = (person) lview.getitematposition(position);
   // toast.maketext(getapplicationcontext(),person.getid().tostring(),1).show();

              // show2()方法对应的处理方法
              // show2方法中,adapter返回的是cursor,
   // cursor cursor = (cursor) lview.getitematposition(position);
   // int personid = cursor.getint(cursor.getcolumnindex("_id"));
   // toast.maketext(getapplicationcontext(), personid + "", 1).show();

              // show1()方法对应的处理方法
   // show1方法中,adapter返回的是map,再对map进行操作取出相应的id值
   hashmap<string, object> item = (hashmap<string, object>) lview.getitematposition(position);
   int personid = (integer) item.get("id");
   toast.maketext(getapplicationcontext(), personid + "", 1).show();
  }
 }

上一篇:

下一篇: