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

  • 相关阅读:
    【Azure 应用服务】在Azure App Service多实例的情况下,如何在应用中通过代码获取到实例名(Instance ID)呢?
    【Azure 应用服务】App Service For Windows 中如何设置代理实现前端静态文件和后端Java Spring Boot Jar包
    【Azure Developer】使用Azure Key Vault 的Key签名后,离线验证的一些参考资料
    【Azure Function】调试 VS Code Javascript Function本地不能运行,报错 Value cannot be null. (Parameter 'provider')问题
    【Azure 应用服务】App Service 使用Tomcat运行Java应用,如何设置前端网页缓存的相应参数呢(Xms512m Xmx1204m)?
    【Azure API 管理】APIM添加Logtoeventhub的策略后,一些相关APIM与Event Hub的问题
    【Azure API 管理】为调用APIM的请求启用Trace 调试APIM Policy的利器
    【Azure 事件中心】China Azure上是否有Kafka服务简答
    【Azure 应用服务】探索在Azure上设置禁止任何人访问App Service的默认域名(Default URL)
    【Azure 微服务】记一次错误的更新Service Fabric 证书而引发的集群崩溃而只能重建
  • 原文地址:https://www.cnblogs.com/harry335/p/4793429.html
Copyright © 2011-2022 走看看