Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid: Accessing and querying properly using Raw Contact Id
    text
    copied!<p>So my app is attempting to integrate a sync adapter to the android native contact manager. This is all running smoothly except once a contact is synced, I am unable to update it. Details on this particular problem can be found here: <a href="https://stackoverflow.com/questions/5288384/android-content-resolver-query-returning-0-rows-when-it-ought-not-too/">Android: Content resolver query returning 0 rows when it ought not to</a> But I can sum it up simply by just saying my content resolver query is returning 0 values because I am querying the wrong URI, I believe.</p> <p>When I write the raw contact id to the phone, I do it with the following code:</p> <pre><code>public ContactSyncOperations(Context context, String username, String accountName, BatchOperationForSync batchOperation) { this(context, batchOperation); mBackReference = mBatchOperation.size(); mIsNewContact = true; mValues.put(RawContacts.SOURCE_ID, username); mValues.put(RawContacts.ACCOUNT_TYPE, "com.tagapp.android"); mValues.put(RawContacts.ACCOUNT_NAME, accountName); mBuilder = newInsertCpo(RawContacts.CONTENT_URI, true).withValues(mValues); mBatchOperation.add(mBuilder.build()); } </code></pre> <p>This constructor method is called when you are adding a new contact to the sync list:</p> <pre><code>private static void addContact(Context context, String account, Contact contact, BatchOperationForSync batchOperation) { ContactSyncOperations contactOp = ContactSyncOperations.createNewContact(context, contact.getUsername(), account, batchOperation);//constructor called @ this line contactOp.addName(contact.getFirstName(), contact.getLastName()); contactOp.addEmail(contact.getEmail()); contactOp.addPhone(contact.getPhoneNumber(), Phone.TYPE_MOBILE); contactOp.addPhone(contact.getHomePhone(), Phone.TYPE_HOME); contactOp.addPhone(contact.getWorkPhone(), Phone.TYPE_WORK); contactOp.addProfileAction(contact.getUsername()); Log.e("Adding contact", "Via sync"); } </code></pre> <p>As you can see in the constructor, I am calling a method called newInsertCpo, which can be viewed here:</p> <pre><code>private void addInsertOp() { if(!mIsNewContact) { mValues.put(Phone.RAW_CONTACT_ID, mRawContactId); } mBuilder = newInsertCpo(addCallerIsSyncAdapterParameter(Data.CONTENT_URI), mYield); mBuilder.withValues(mValues); if(mIsNewContact) { mBuilder.withValueBackReference(Data.RAW_CONTACT_ID, mBackReference); } mYield = false; mBatchOperation.add(mBuilder.build()); } </code></pre> <p>Now that you can see the code, let me explain the problem. When I am creating and writing the raw contact id, I am doing so to RawContacts.CONTENT_URI:</p> <pre><code>mBuilder = newInsertCpo(RawContacts.CONTENT_URI, true).withValues(mValues); </code></pre> <p>However, when I query the uri, I am querying as so:</p> <pre><code>Cursor cursor = resolver.query(Data.CONTENT_URI, DataQuery.PROJECTION, DataQuery.SELECTION, new String[] {String.valueOf(rawContactId)}, null); </code></pre> <p>From Data.CONTENT_URI. I believe this is where my problem is occurring. I used the sample code from Android's official dev site and tailored it for my own uses, but this part is pretty true to the example. Yet it is not working. My lead is what I said, I'm writing to one Uri and querying another. But I've attempted to change the query to RawContacts.CONTENT_URI (which caused an exception), and also changing the Uri I write to Data.CONTENT_URI, which also causes an exception. What's even more confusing is that I get raw contact ids from Data.CONTENT_URI when I do my lookupRawContactId method:</p> <pre><code>private static long lookupRawContact(ContentResolver resolver, String username) { Log.e("Looking up Raw Contact", username); long authorId = 0; Cursor cursor = resolver.query(Data.CONTENT_URI, UserIdQuery.PROJECTION, UserIdQuery.SELECTION, new String[] {username}, null); try { if(cursor != null &amp;&amp; cursor.moveToFirst()) { authorId = cursor.getLong(UserIdQuery.COLUMN_ID); } } finally { if(cursor != null) { cursor.close(); } } return authorId; } </code></pre> <p>So yea, if I get a cursor returning rawcontactId, why would I get 0 values querying that same Uri with that same rawcontactId i was returned? It doesn't make any sense! Does anyone have any insight? Thanks all.</p>
 

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