Android之ListView使用方法-SimpleAdapter-SimpleCursorAdapter

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

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

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

显示效果如图所示:

 

界面布局:

item.xml

[html]  view plain  copy
  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.   <!-- 存款 -->  
  21.   <TextView  
  22.    android:layout_width="fill_parent"  
  23.    android:layout_height="wrap_content"  
  24.    android:id="@+id/amount"  
  25.   />  
  26. </LinearLayout>  


main.xml

[html]  view plain  copy
  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进行数据绑定

[html]  view plain  copy
  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进行数据绑定

[html]  view plain  copy
  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://blog.csdn.net/furongkang/article/details/6819247感谢感恩

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值