【发布时间】:2012-04-12 15:51:16
【问题描述】:
我有一个电话号码数组,我想从联系人数据库中获取相应的联系人姓名。
在电话号码数组中,我还有一些以前没有保存到联系人数据库中的号码。例如;
- 3333333 -> 蒂姆
- 5555555 -> 吉姆
- 1111111 -> 未知
我有一个包含上面显示的电话号码的数组,即 phoneArr。
int size=phoneArr.size();
if(size>0){
Cursor[] cursors=new Cursor[size];
for(int i=0;i<size;i++){
Uri contactUri1 = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneArr.get(i)));
cursors[i] = getContentResolver().query(contactUri1, PEOPLE_PROJECTION, null, null, " _id asc limit 1");
}
Cursor phones=new MergeCursor(cursors);
phones.getCount() 在上述情况下返回 2。当电话号码未出现在联系人列表中时,光标变为空,并且当我合并它们时,它根本没有任何贡献。我想要的是有一个光标如下
光标电话 -> {Tim, Jim, 1111111}
我想我可以通过手动添加行来做到这一点,如下所示:
Uri contactUri1 = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneArr.get(i)));
cursors[i] = getContentResolver().query(contactUri1, PEOPLE_PROJECTION, null, null, " _id asc limit 1");
if(cursors[i].getCount()==0)
// add the phone number manually to the cursor
我怎样才能做到这一点?
这里是 PEOPLE_PROJECTION
private static final String[] PEOPLE_PROJECTION = new String[] {
ContactsContract.PhoneLookup._ID,
ContactsContract.PhoneLookup.DISPLAY_NAME,
ContactsContract.PhoneLookup.NUMBER
};
【问题讨论】:
-
请查看您接受的答案,根据Android SDK目前可用的功能似乎是错误的。
-
好吧,当我接受答案时,这似乎是合理的。有空我会复习的。
标签: android row android-cursor