私は、指定されたサーバから連絡先を同期するアプリケーションを作成したいと考えています。これらの連絡先を特定するために、アンドロイドの連絡先に識別子を隠しフィールドに格納したいと考えています。アンドロイド生の連絡先に隠し同期識別子を格納する
フィールドSYNC1が適切と思われますが、このフィールドを正しく使用する方法はわかりません。
私は、次の方法でそれを設定してみました:
ArrayList<ContentProviderOperation> ops =
new ArrayList<>();
int rawContactInsertIndex = ops.size();
ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
.withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, accountType)
.withValue(ContactsContract.RawContacts.ACCOUNT_NAME, ACCOUNT_NAME)
.withValue(ContactsContract.RawContacts.SYNC1, "myInternalId")
.build());
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.RawContacts.Data.RAW_CONTACT_ID, rawContactInsertIndex)
.withValue(ContactsContract.RawContacts.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, "Test User")
.build());
getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
問題は、バッチ挿入が通ることを、今ですが、私がしようとすると、それが空であるため、フィールドが、接触して設定されていません後で読む:
ContentResolver cr = this.getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(cur.getColumnIndex(
ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(
ContactsContract.Contacts.DISPLAY_NAME));
String sync1 = null;
int sync1Index = cur.getColumnIndex(ContactsContract.RawContacts.SYNC1);
if (sync1Index >= 0) {
sync1 = cur.getString(sync1Index);
}
System.out.println("Contact id=" + id + " name=" + name + " sync1=" + sync1);
}
}
cur.close();
生の連絡先に設定する場合、2番目のバッチでは例外が発生します。
が空であるか、<0 sync1Index? ...クエリの2番目のパラメータとしてnullを渡すのは良い考えではありません...あなたがwanaを取得する列を渡す方がいいです...また、クエリのすべての連絡先を取得していますアカウントの種類 – Selvin
syncIndex < 0、結果はこの列を含まない – reini122