ListView之SimpleCursorAdapter列表--3

本文介绍了如何使用SimpleCursorAdapter结合Cursor查询数据库,将通讯录数据展示在ListView中。通过设置权限,获取Contacts.CONTENT_URI的Cursor,然后利用SimpleCursorAdapter自动映射数据到TextView,展示了数据绑定的基本过程。在实际应用中遇到电话号码无法获取的问题,可能与API更新有关,需进一步研究。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

SimpleCursorAdapter需要造作数据库,一般通讯录Contacts就是用这种适配器,就是从Cursor游标里取得的数据用ListView显示,并可以把指定的列表映射到TextView中(这里的TextView是 android.R.layout.simple_expandable_list_item_1.xml),文件如下(系统自带的)

Java代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <TextView  
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     android:id="@android:id/text1"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="?android:attr/listPreferredItemHeight"  
  7.     android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"       
  8.     android:textAppearance="?android:attr/textAppearanceLarge"  
  9.     android:gravity="center_vertical"  
  10. />  
 

 

Cursor cursor = getContentResolver().query(People.CONTENT_URI, null, null, null, null);先获得一个指向系统通讯录数据库的Cursor对象获得数据来源。

 startManagingCursor(cursor);我们将获得的Cursor对象交由Activity管理,这样Cursor的生命周期和Activity便能够自动同步,省去自己手动管理Cursor。

 SimpleCursorAdapter 构造函数前面3个参数和ArrayAdapter是一样的,最后两个参数:一个包含数据库的列的String型数组,一个包含布局文件中对应组件id的 int型数组。其作用是自动的将String型数组所表示的每一列数据映射到布局文件对应id的组件上。上面的代码,将NAME列的数据一次映射到布局文件的id为text1的组件上。

注意:需要在AndroidManifest.xml中如权限:<uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>

否则会出错

 

本来想做一个可以显示联系人姓名和电话号码的例子,但是电话号码总是去不出来,运行就出错,1.5以后联系人的API跟以前的不一样,弄了一天,还是有很多错误,只能暂停了,以后再慢慢的去了解

SimpelCursorAdapterTest.java

Java代码  收藏代码
  1. /* 
  2.  * @author loulijun 
  3.  */  
  4. package org.hualang.SimpleCursorAdapterTest;  
  5.   
  6. import android.app.Activity;  
  7. import android.database.Cursor;  
  8. import android.os.Bundle;  
  9. import android.provider.Contacts.People;  
  10. import android.widget.ListView;  
  11. import android.widget.SimpleCursorAdapter;  
  12.   
  13. public class SimpleCursorAdapterTest extends Activity {  
  14.     /** Called when the activity is first created. */  
  15.     private ListView listview;  
  16.     private Cursor cursor;  
  17.     private SimpleCursorAdapter adapter;  
  18.     @Override  
  19.     public void onCreate(Bundle savedInstanceState) {  
  20.         super.onCreate(savedInstanceState);  
  21.         setContentView(R.layout.main);  
  22.         listview=(ListView)findViewById(R.id.listview);  
  23.         cursor=getContentResolver().query(People.CONTENT_URI, nullnullnullnull);  
  24.         startManagingCursor(cursor);  
  25.         adapter=new SimpleCursorAdapter(this,  
  26.                 android.R.layout.simple_expandable_list_item_1,  
  27.                 cursor,  
  28.                 new String[] {People.NAME},  
  29.                 new int[] {android.R.id.text1});  
  30.         listview.setAdapter(adapter);  
  31.     }  
  32. }  

 main.xml

Java代码  收藏代码
  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. <TextView    
  8.     android:layout_width="fill_parent"   
  9.     android:layout_height="wrap_content"   
  10.     android:text="@string/hello"  
  11.     />  
  12. <ListView  
  13.     android:id="@+id/listview"  
  14.     android:layout_width="fill_parent"  
  15.     android:layout_height="wrap_content"  
  16. />  
  17. </LinearLayout>  

 运行结果如下:


可以通过以下步骤将SQLite数据库中的已转换为byte类型的图片显示在ListView中: 1. 创建一个包含图片和其他数据的SQLite数据库表格。 2. 在应用程序中查询该数据库,并将结果存储在Cursor对象中。 3. 使用SimpleCursorAdapter将Cursor对象中的数据绑定到ListView上。 4. 在SimpleCursorAdapter中指定一个ViewBinder,以便在绑定时将字节数组转换为位图并将其设置为ImageView的图像。 以下是实现步骤的示例代码: 1. 创建数据库表格 ```sql CREATE TABLE mytable ( _id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, image BLOB ); ``` 2. 查询数据库 ```java Cursor cursor = db.rawQuery("SELECT * FROM mytable", null); ``` 3. 绑定数据到ListView ```java String[] from = { "name", "image" }; int[] to = { R.id.text_name, R.id.image_view }; SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.list_item, cursor, from, to); adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() { @Override public boolean setViewValue(View view, Cursor cursor, int columnIndex) { if (view.getId() == R.id.image_view) { byte[] imageBytes = cursor.getBlob(columnIndex); Bitmap bitmap = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length); ImageView imageView = (ImageView) view; imageView.setImageBitmap(bitmap); return true; } return false; } }); listView.setAdapter(adapter); ``` 在以上代码中,我们指定了一个ViewBinder来处理ImageView的图像。在ViewBinder的setViewValue()方法中,我们检查当前的视图是否为ImageView,如果是,则将字节数组转换为位图并将其设置为ImageView的图像。 希望能对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值