手动向游标添加行 [英] Adding rows into Cursor manually

查看:195
本文介绍了手动向游标添加行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个电话号码数组,我想从联系人数据库中获取相应的联系人姓名。



在电话号码数组中,未存储到联系人数据库之前的数字。例如:




  • 3333333 - > Tim

  • 5555555 - > Jim

  • 1111111 - >未知



我拥有包含上述电话号码的数组,即 phoneArr

  int size = phoneArr.size(); 
if(size> 0){
Cursor [] cursors = new Cursor [size];
for(int i = 0; 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)
//手动添加电话号码到游标



这里是PEOPLE_PROJECTION



<$> p $ p> private static final String [] PEOPLE_PROJECTION = new String [] {
ContactsContract.PhoneLookup._ID,
ContactsContract.PhoneLookup.DISPLAY_NAME,
ContactsContract.PhoneLookup .NUMBER
};


解决方案

在游标中添加行的最简单方法是使用 MatrixCursor MergeCursor
这两个类是从SDK和这里解决那种问题。



基本上你做的是:


  1. 将您要添加的行放入 MatrixCusror

  2. code> cursor 和您的 matrixCursor 使用 MergeCursor

如下:

  //创建MatrixCursor填充您要添加的行。 
MatrixCursor matrixCursor = new MatrixCursor(new String [] {colName1,colName2});
matrixCursor.addRow(new Object [] {value1,value2});

//使用您创建的matrixCursor合并现有的游标。
MergeCursor mergeCursor = new MergeCursor(new Cursor [] {m​​atrixCursor,cursor});

//使用你的游标mergeCursor。


I have an array of phone numbers and I want to get the corresponding contact names from the contacts database.

In the array of phone numbers, I also have some numbers that are not saved before to the contact database. For example;

  • 3333333 -> Tim
  • 5555555 -> Jim
  • 1111111 -> unknown

I have the array containing the phone numbers shown above, namely 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() returns 2 in the above scenario. When the phone number does not appear in the contact list the cursor becomes empty and somehow when I merge them it doesn't contribute anything at all. What I want is to have a cursor as follows

Cursor phones -> {Tim, Jim, 1111111}

I think I can do this by adding the row manually as follows:

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

How can I achieve this?

Here is the PEOPLE_PROJECTION

private static final String[] PEOPLE_PROJECTION = new String[] {
    ContactsContract.PhoneLookup._ID,
    ContactsContract.PhoneLookup.DISPLAY_NAME,
    ContactsContract.PhoneLookup.NUMBER
};

解决方案

Easiest way to add rows in a cursor is to use a MatrixCursor and a MergeCursor. Those two classes are from the SDK and here to solve that kind of problems.

Basically what you do is :

  1. Put the rows you want to add in a MatrixCusror
  2. Merge your cursor and your matrixCursor using a MergeCursor

Something like:

// Create a MatrixCursor filled with the rows you want to add.
MatrixCursor matrixCursor = new MatrixCursor(new String[] { colName1, colName2 });
matrixCursor.addRow(new Object[] { value1, value2 });

// Merge your existing cursor with the matrixCursor you created.
MergeCursor mergeCursor = new MergeCursor(new Cursor[] { matrixCursor, cursor });

// Use your the mergeCursor as you would use your cursor.

这篇关于手动向游标添加行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆