zoukankan      html  css  js  c++  java
  • Android采用ListView实现数据列表显示1-使用SimpleCursorAdapter进行数据绑定

    Android采用ListView实现数据列表显示

     

    要将数据库中的数据列表显示在屏幕上,我们要使用ListView这个控件,当用户从数据库中取出数据时,要将数据绑定到显示控件上,如何绑定呢,我们需要创建适配器进行绑定,创建适配器有两种方式:

    第一种是用SimpleAdapter创建(要求绑定的数据是List<HashMap<String, Object>>数据类型)

    第二种是用SimpleCursorAdapter创建(要求绑定的数据是Cursor数据类型)

    显示效果如图所示:

    界面布局:

    item.xml

    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <!--item -->  
    3. <LinearLayout  
    4.   xmlns:Android="http://schemas.android.com/apk/res/android"  
    5.   android:orientation="horizontal"  
    6.   android:layout_width="fill_parent"  
    7.   android:layout_height="fill_parent">  
    8.   <!-- 名称 -->  
    9.   <TextView  
    10.    android:layout_width="130dp"  
    11.    android:layout_height="wrap_content"  
    12.    android:id="@+id/name"  
    13.   />  
    14.   <!-- 电话 -->  
    15.   <TextView  
    16.    android:layout_width="150dp"  
    17.    android:layout_height="wrap_content"  
    18.    android:id="@+id/phone"  
    19.   />  
    20.   <!-- 存款11 -->  
    21.   <TextView  
    22.    android:layout_width="fill_parent"  
    23.    android:layout_height="wrap_content"  
    24.    android:id="@+id/amount"  
    25.   />  
    26. </LinearLayout>  

    main.xml

    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    3.     android:orientation="vertical"  
    4.     android:layout_width="fill_parent"  
    5.     android:layout_height="fill_parent"  
    6.     >  
    7.  <!-- 标题 -->  
    8.  <LinearLayout  
    9.   android:orientation="horizontal"  
    10.   android:layout_width="fill_parent"  
    11.   android:layout_height="wrap_content">  
    12.     
    13.   <TextView  
    14.    android:layout_width="130dp"  
    15.    android:layout_height="wrap_content"  
    16.    android:text="姓名"  
    17.   />  
    18.    
    19.    <TextView  
    20.    android:layout_width="150dp"  
    21.    android:layout_height="wrap_content"  
    22.    android:text="电话"  
    23.   />  
    24.     
    25.   <TextView  
    26.    android:layout_width="fill_parent"  
    27.    android:layout_height="wrap_content"  
    28.    android:text="存款"  
    29.   />  
    30.      
    31. </LinearLayout>  
    32.  <!-- ListView控件 -->  
    33. <ListView    
    34.     android:layout_width="fill_parent"   
    35.     android:layout_height="fill_parent"   
    36.     android:id="@+id/listView"  
    37.     />  
    38. </LinearLayout>  

    使用SimpleAdapter进行数据绑定

    1. public class MainActivity extends Activity {  
    2.     private PersonService service;  
    3.     @Override  
    4.     public void onCreate(Bundle savedInstanceState) {  
    5.         super.onCreate(savedInstanceState);  
    6.         setContentView(R.layout.main);  
    7.         service = new PersonService(this);  
    8.         ListView listView = (ListView) this.findViewById(R.id.listView);  
    9.           
    10.         //获取到集合数据  
    11.         List<Person> persons = service.getScrollData(0, 10);  
    12.         List<HashMap<String, Object>> data = new ArrayList<HashMap<String,Object>>();  
    13.         for(Person person : persons){  
    14.             HashMap<String, Object> item = new HashMap<String, Object>();  
    15.             item.put("id", person.getId());  
    16.             item.put("name", person.getName());  
    17.             item.put("phone", person.getPhone());  
    18.             item.put("amount", person.getAmount());  
    19.             data.add(item);  
    20.         }  
    21.        //创建SimpleAdapter适配器将数据绑定到item显示控件上  
    22.        SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.item,   
    23.                 new String[]{"name", "phone", "amount"}, new int[]{R.id.name, R.id.phone, R.id.amount});  
    24.        //实现列表的显示  
    25.        listView.setAdapter(adapter);  
    26.        //条目点击事件  
    27.        listView.setOnItemClickListener(new ItemClickListener());  
    28.     }  
    29.        //获取点击事件      
    30.     private final class ItemClickListener implements OnItemClickListener{  
    31.   
    32.         public void onItemClick(AdapterView<?> parent, View view, int position, long id) {  
    33.             ListView listView = (ListView) parent;  
    34.             HashMap<String, Object> data = (HashMap<String, Object>) listView.getItemAtPosition(position);  
    35.             String personid = data.get("id").toString();  
    36.             Toast.makeText(getApplicationContext(), personid, 1).show();  
    37.         }  
    38.     }  
    39. }  

    使用SimpleCursorAdapter进行数据绑定

    1. public class MainActivity extends Activity {  
    2.     private PersonService service;  
    3.     @Override  
    4.     public void onCreate(Bundle savedInstanceState) {  
    5.         super.onCreate(savedInstanceState);  
    6.         setContentView(R.layout.main);  
    7.         service = new PersonService(this);  
    8.         ListView listView = (ListView) this.findViewById(R.id.listView);  
    9.         //获取游标  
    10.         Cursor cursor = service.getCursorScrollData(0, 10);  
    11.         //创建SimpleCursorAdapter适配器将数据绑定到item显示控件上  
    12.         SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.item, cursor,   
    13.                 new String[]{"name", "phone", "amount"}, new int[]{R.id.name, R.id.phone, R.id.amount});  
    14.         listView.setAdapter(adapter);  
    15.         //条目点击事件  
    16.         listView.setOnItemClickListener(new ItemClickListener());  
    17.     }  
    18.       
    19.     private final class ItemClickListener implements OnItemClickListener{  
    20.   
    21.         public void onItemClick(AdapterView<?> parent, View view, int position, long id) {  
    22.             ListView listView = (ListView) parent;  
    23.             Cursor cursor = (Cursor) listView.getItemAtPosition(position);  
    24.             String personid = String.valueOf(cursor.getInt(cursor.getColumnIndex("_id")));  
    25.             Toast.makeText(getApplicationContext(), personid, 1).show();  
    26.         }  
    27.     }  
    28. }  

    注意:使用第二种方式在获取数据集合时必须指定主键"_id"

    转自:http://www.linuxidc.com/Linux/2011-09/43938.htm

  • 相关阅读:
    是否有人会想起
    春风下也有落叶
    to kongove:呵呵~是啊,偶尔吹得玩下,仅当娱乐而已……
    木头人の狂想
    夜行静思湖
    [学习标准库]math.h
    [学习标准库]ctype.h
    [学习标准库]stdio.h
    句子
    [学习标准库]assert.h
  • 原文地址:https://www.cnblogs.com/harry335/p/4793429.html
Copyright © 2011-2022 走看看