Note that there are some explanatory texts on larger screens.

plurals
  1. POAdding rows into Cursor manually
    primarykey
    data
    text
    <p>I have an array of phone numbers and I want to get the corresponding contact names from the contacts database.</p> <p>In the array of phone numbers, I also have some numbers that are not saved before to the contact database. For example;</p> <ul> <li>3333333 -> Tim</li> <li>5555555 -> Jim</li> <li>1111111 -> unknown</li> </ul> <p>I have the array containing the phone numbers shown above, namely <em>phoneArr</em>. </p> <pre><code>int size=phoneArr.size(); if(size&gt;0){ Cursor[] cursors=new Cursor[size]; for(int i=0;i&lt;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); </code></pre> <p>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</p> <p><strong>Cursor phones -> {Tim, Jim, 1111111}</strong></p> <p>I think I can do this by adding the row manually as follows:</p> <pre><code>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 </code></pre> <p>How can I achieve this?</p> <p>Here is the PEOPLE_PROJECTION</p> <pre><code>private static final String[] PEOPLE_PROJECTION = new String[] { ContactsContract.PhoneLookup._ID, ContactsContract.PhoneLookup.DISPLAY_NAME, ContactsContract.PhoneLookup.NUMBER }; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload