Android:插入后获取联系人ID [英] Android: get contact id after insert
问题描述
我需要在创建新联系人后存储联系人ID值,以便能够在其他时候引用它.例如,我创建一个新联系人,此后我要从其联系人ID中删除它,因此在创建新联系人之后,我需要检索联系人ID值.这是我创建新联系人的方式:
I need to store the contact id value after create a new contact, in order to be able to reference it in other moment. For example, I create a new contact, and after that I want to delete it from its contact id, so I need to retrieve the contact id value after create a new contact. This is how I create new contacts:
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI).withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, tipoCuenta).withValue(ContactsContract.RawContacts.ACCOUNT_NAME, cuenta).build());
//Insert some data here....
c.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
//Here, I want to retrieve contact id
我该怎么做?
推荐答案
ContentProviderResult 对象,每个操作一个.每个都有插入的联系人的uri(格式为content://com.android.contacts/raw_contacts/<contact_id>
).
The ContentResolver.applyBatch() method returns an array of ContentProviderResult objects, one for each operation. Each of these has the uri of the inserted contact (in the format content://com.android.contacts/raw_contacts/<contact_id>
).
因此要获取联系人的ID,您只需解析此uri,即
So to get the contact's id you just have to parse this uri, i.e.
ContentProviderResult[] results = getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
int contactId = Integer.parseInt(results[0].uri.getLastPathSegment());
这篇关于Android:插入后获取联系人ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!