Android查询电话薄报错原因分析

Android2.0之后的版本中,对通讯录列表contact中的内容做了一些改进,number这个属性被移动到了另一地方(Uri: ContactsContract.CommonDataKinds.Phone.CONTENT_URI)。当我们学习关于Android的教程或在网上查看这方面的资料时,提供的程序大部分如下:

Cursor cur = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);

startManagingCursor(cur);

ListAdapter adapter = new SimpleCursorAdapter(

this,

android.R.layout.expandable_list_content,  

cur,

new String[]{PhoneLookup.DISPLAY_NAME,PhoneLookup.NUMBER},

new int[]{android.R.id.text1,android.R.id.text2});

listView.setAdapter(adapter);

......

然而,每次运行这段程序之后都会报错,提示source not found, Caused by: java.lang.IllegalArgumentException: column 'number' does not exist。如果我们将查询列PhoneLookup.NUMBER替换为别的列信息或删除,则程序可以正常运行。因此,为了能够显示电话号码信息,我们需要到ContactsContract.CommonDataKinds.Phone.CONTENT_URI中获取号码信息,如下:

 

ArrayList list = new ArrayList<Map<String, String>>(); //记录电话列表信息

while (cur.moveToNext()) {

Map<String, String> map = new HashMap<String, String>();

 

long id = cur.getLong(cur.getColumnIndex("_id"));

 

StringBuilder buf = new StringBuilder(ContactsContract.CommonDataKinds.Phone.CONTACT_ID);

buf.append("=").append(id);

Cursor pcur = getContentResolver().query(

ContactsContract.CommonDataKinds.Phone.CONTENT_URI,

null, buf.toString(), null, null);

//移动pcur游标,查找所有的电话列表

//......

pcur.close();

 

//将联系人姓名和电话信息组成键值对放入map中,然后添加到list中

}

cur.close();

 

ListAdapter adapter = new SimpleAdapter(

this,

list,

android.R.layout.simple_list_item_2, 

         new String[]{ NAME, NUMBER }, 

         new int[]{ android.R.id.text1, android.R.id.text2 });

......

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值