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

2016-02-19 10:10 14 1 收藏

有一种朋友不在生活里,却在生命力;有一种陪伴不在身边,却在心间。图老师即在大家的生活中又在身边。这么贴心的服务你感受到了吗?话不多说下面就和大家分享Android ListView数据绑定显示的三种解决方法吧。

【 tulaoshi.com - 编程语言 】

首先,创建一个用于显示一个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);
  ListHashMapString, Object data = new ArrayListHashMapString, Object();
  for (Person person : persons) {
   HashMapString, Object item = new HashMapString, 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();

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

              // show2()方法对应的处理方法
              // show2方法中,adapter返回的是Cursor,
   // Cursor cursor = (Cursor) lview.getItemAtPosition(position);
   // int personid = cursor.getInt(cursor.getColumnIndex("_id"));
   // Toast.makeText(getApplicationContext(), personid + "", 1).show();

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

              // show1()方法对应的处理方法
   // show1方法中,adapter返回的是Map,再对Map进行操作取出相应的id值
   HashMapString, Object item = (HashMapString, Object) lview.getItemAtPosition(position);
   int personid = (Integer) item.get("id");
   Toast.makeText(getApplicationContext(), personid + "", 1).show();
  }
 }

来源:https://www.tulaoshi.com/n/20160219/1593633.html

延伸阅读
标签: Web开发
创建带有数据的互动网页很简单,只需通过拖拉,设置属性并储存页面即可,并不需要编程,不过Visual InterDev提供了一个完整的对象模型,可让你调整应用程序,进行用户确认并控制Web应用程序,也支持DHTML及IE4数据控制以获得更丰富的用户界面,要生成一个使用数据控件创建的简单数据输入页面的步骤如下: (1),在创建一个新的数据连接后,将...
标签: 电脑入门
很多用户都会碰到开机的时候显示器不显示,显示无信号的状态,这是有很多种的原因照成。 第一种情况,确实是你的显示器坏了,这种情况我们要怎么去判断呢?只有用最简单的交换法去确认了。拿一台别的显示器,换到这台主机上,看看是否有显示。如果没有多的显示器,就可以拿笔记本电脑,把这台显示器外接到笔记本电脑上,来看下是否能显示。 ...
据说dl-ssl.google.com在大陆被强了,伟大的天朝真是不让人活了,解决方法就是修改C:\Windows\System32\drivers\etc\hosts文件。添加一行: 74.125.237.1       dl-ssl.google.com  再次下载SDK的时候就会成功啦!
标签: PHP
  1. $row = mysql_fetch_row($result); 返回一个规则的数组$row,$row[0]是第一个元素,$row[1]是第二个元素,依次类推... mysql_num_fields($result) 返回结果的元素个数。 2. $row = mysql_fetch_array($result); 返回一个数组$row. 举例如下: 表结构如下: username | password ------------------------------------- bourbon...
在Android开发中,定时器一般有以下3种实现方法: 一、采用Handler与线程的sleep(long)方法 二、采用Handler的postDelayed(Runnable, long)方法 三、采用Handler与timer及TimerTask结合的方法 下面逐一介绍: 一、采用Handle与线程的sleep(long)方法 Handler主要用来处理接受到的消息。这只是最主要的方法,当然Handler里还有其他的方法供实现...

经验教程

334

收藏

58
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部