Android - 新数据记录添加到错误的联系人

发布于 2024-10-19 16:54:33 字数 2287 浏览 4 评论 0原文

我正在尝试将数据记录添加到已存在的联系人中,我使用电话查找找到联系人,我获取联系人 _id 字段,然后添加将 raw_contact_id 设置为 _id 字段的新数据。 在某些联系人上它不起作用,它将数据匹配到不同的联系人。 (我认为这与存储在SIM卡上的联系人有关)

请建议,也许您有不同的方式来添加数据

代码示例: <代码>

LinkedList<Long> lcv = new LinkedList<Long>();
    ContentResolver cr = getContentResolver();
    Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
    Cursor c = cr.query(uri, null, null, null, null);
    try {
        while (c.moveToNext()) {
            Uri lookupUri = Uri.withAppendedPath(Contacts.CONTENT_LOOKUP_URI,
                    c.getString(c.getColumnIndex(PhoneLookup.LOOKUP_KEY)));
            Cursor c2 = getContentResolver().query(lookupUri, new String[] { Contacts._ID, Contacts.DISPLAY_NAME },
                    null, null, null);
            try {
                if (c2.moveToNext()) {
                    Log.i(TAG, "found: " + c2.getLong(c2.getColumnIndex(Contacts._ID)) + ", " + c2.getString(c2.getColumnIndex(Contacts.DISPLAY_NAME)));
                    lcv.add(c2.getLong(c2.getColumnIndex(Contacts._ID)));
                } else {
                    Log.e(TAG, "failed to lookup");
                }
            } finally {
                c2.close();
            }
        }
    } finally {
        c.close();
    }

    for (Long rawid : lcv) {
                        Cursor c3 = cr.query(RawContacts.CONTENT_URI, null, RawContacts.CONTACT_ID + "=?", new String[] {rawid+""}, null);
                        if (c3.moveToNext()) {
                            Log.e(TAG,"aaaa: " + c3.getString(c3.getColumnIndex(Contacts.DISPLAY_NAME)));
                        } else {
                            Log.e(TAG,"errrrror");
                        }

                        ContentValues cv = new ContentValues();
                        cv.put(Data.RAW_CONTACT_ID, rawid + "");
                        cv.put(Data.MIMETYPE, MyMime.MIMETYPE);
                        cv.put(Data.DATA1, "mydata");
                        cv.put(Data.SYNC1, syncvalue);
                        Uri newIns = cr.insert(ContactsContract.Data.CONTENT_URI, cv);
                        Log.i(TAG, "insert: " + newIns + ", " + name);
                    }

<代码>

I'm trying to add Data record to an already exist contact, I find the contact using phone lookup, i take the contact _id field, and add a new data with raw_contact_id set to the _id field.
on some contacts it just doesn't work, it match the data to different contact.
(I think it relates to contacts that are stored on the sim card)

Please advice, maybe you have a different way to add the data

code sample:

LinkedList<Long> lcv = new LinkedList<Long>();
    ContentResolver cr = getContentResolver();
    Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
    Cursor c = cr.query(uri, null, null, null, null);
    try {
        while (c.moveToNext()) {
            Uri lookupUri = Uri.withAppendedPath(Contacts.CONTENT_LOOKUP_URI,
                    c.getString(c.getColumnIndex(PhoneLookup.LOOKUP_KEY)));
            Cursor c2 = getContentResolver().query(lookupUri, new String[] { Contacts._ID, Contacts.DISPLAY_NAME },
                    null, null, null);
            try {
                if (c2.moveToNext()) {
                    Log.i(TAG, "found: " + c2.getLong(c2.getColumnIndex(Contacts._ID)) + ", " + c2.getString(c2.getColumnIndex(Contacts.DISPLAY_NAME)));
                    lcv.add(c2.getLong(c2.getColumnIndex(Contacts._ID)));
                } else {
                    Log.e(TAG, "failed to lookup");
                }
            } finally {
                c2.close();
            }
        }
    } finally {
        c.close();
    }

    for (Long rawid : lcv) {
                        Cursor c3 = cr.query(RawContacts.CONTENT_URI, null, RawContacts.CONTACT_ID + "=?", new String[] {rawid+""}, null);
                        if (c3.moveToNext()) {
                            Log.e(TAG,"aaaa: " + c3.getString(c3.getColumnIndex(Contacts.DISPLAY_NAME)));
                        } else {
                            Log.e(TAG,"errrrror");
                        }

                        ContentValues cv = new ContentValues();
                        cv.put(Data.RAW_CONTACT_ID, rawid + "");
                        cv.put(Data.MIMETYPE, MyMime.MIMETYPE);
                        cv.put(Data.DATA1, "mydata");
                        cv.put(Data.SYNC1, syncvalue);
                        Uri newIns = cr.insert(ContactsContract.Data.CONTENT_URI, cv);
                        Log.i(TAG, "insert: " + newIns + ", " + name);
                    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

安人多梦 2024-10-26 16:54:33

问题在于当您选择 Contacts._ID 并使用此 id 填充 LinkedList lcv 中的数据时。

 Cursor c2 = getContentResolver().query(lookupUri, new String[] { Contacts._ID, Contacts.DISPLAY_NAME },
                    null, null, null);

您实际上在这里需要一个RAW_CONTACT_ID

可以从联系人database/ContactsContract.Data”或“database/ContactsContract.CommonDataKinds.StructuredName”或“database/RawContactsEntity”获取DISPLAY_NAME。在后两种情况下,您将能够使用 RAW_CONTACT_ID

几个关键指针来获取 DISPLAY_NAME

  • Contacts._ID = Data.CONTACT_ID
  • RawContacts._ID = Data.RAW_CONTACT_ID
  • RawContacts。 CONTACT_ID = 联系人._ID
  • RawContactsEntity._ID = RawContacts._ID

听起来很混乱?让我试试...

  1. 联系人数据库分为 3 个表contacts原始联系人原始联系人 >数据
  2. 每个表都包含列 (_ID),它是自动递增的主键。
  3. 数据表包含所有联系信息,如电话号码、邮件 ID、地址等。
  4. 原始联系人指向创建的实际联系人。因此,我们在添加联系人时使用原始联系人
  5. 用户无法在联系人表中添加任何数据。由于联系人聚合,此表中的数据是在内部填充的一个>。
  6. 您的逻辑对某些联系人起作用的原因是:联系人的_ID,原始联系人保持不变,直到出现是否发生任何接触聚集。假设您添加了两个同名 abc 的联系人。这里,原始联系人的 _ID 增加两次,而联系人的 _ID 仅增加一次,因为这两个联系人因合并而增加到联系人聚合

请参阅 了解更多详细信息。

在您的情况下获取信息的最佳方法是使用 ContactsContract.RawContactsEntity ( raw_contacts 表与数据表的外部联接)

参考:http://developer.android.com/reference/android/provider/ContactsContract.RawContactsEntity.html

The problem lies when you select the Contacts._ID and use this id to populate the data in the LinkedList lcv .

 Cursor c2 = getContentResolver().query(lookupUri, new String[] { Contacts._ID, Contacts.DISPLAY_NAME },
                    null, null, null);

You actually need a RAW_CONTACT_ID here.

The DISPLAY_NAME can be fetched either from Contacts database/ContactsContract.Data' OR 'database/ContactsContract.CommonDataKinds.StructuredName' OR 'database/RawContactsEntity. In the later 2 cases you will be able to fetch the DISPLAY_NAME using RAW_CONTACT_ID

Couple of Key pointers:

  • Contacts._ID = Data.CONTACT_ID
  • RawContacts._ID = Data.RAW_CONTACT_ID
  • RawContacts.CONTACT_ID = Contacts._ID
  • RawContactsEntity._ID = RawContacts._ID

Sounds confusing?? Let me try...

  1. The Contacts database is divided into 3 tables contacts, raw contacts, and data.
  2. Each table contains column (_ID) which is an auto incremented primary key.
  3. data table contains all the contact info like phone number, mail id, address etc.
  4. The raw contacts points to the actual contact created. Hence we use the raw contacts while adding a contact.
  5. The user cannot add any data in the contacts table. The data in this table is populated internally due to aggregation of contacts.
  6. The reason your logic worked for some of the contacts is: _ID for contacts, raw contacts remains same until there is any contact aggregation taking place. Lets say you add two contacts with same name abc. Here the _ID for raw contacts increments twice while _ID for contacts increments only once as these two contacts gets merged due to the aggregation of contacts

Refer this for more details.

The best approach to fetch the info in your case is by using ContactsContract.RawContactsEntity ( an outer join of the raw_contacts table with the data table)

Reference: http://developer.android.com/reference/android/provider/ContactsContract.RawContactsEntity.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文