Android的 - 新数据记录被添加到了错误的接触到了、错误、数据、Android

2023-09-13 23:39:45 作者:你个撸不死的

我想数据记录添加到已经存在的接触,我发现接触用电话查询,我把联系人_id领域,并与raw_contact_id组的_id字段添加一个新的数据。 一些接触,它只是不工作,它匹配数据,以不同的接触。 (我认为这涉及到存储在SIM卡上的联系人)

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样品:

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);
                    }

推荐答案

在您选择的问题就出在 Contacts._ID 并用这个ID来填充数据LinkedList的轻型商用车。

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);

您确实需要一个 RAW_CONTACT_ID 在这里。

You actually need a RAW_CONTACT_ID here.

DISPLAY_NAME 既可以从联系人数据库/ ContactsContract.Data'或'数据库/ ContactsContract.CommonDataKinds.StructuredName'或'数据库/读取RawContactsEntity 。在后来的2箱子您将能够获取 DISPLAY_NAME 使用 RAW_CONTACT_ID

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

夫妇的主要指针:

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...

联系人数据库被分成3个表的的联系人的 原料接触的和 数据的 每个表包含柱(_ID),是一种自动递增的主键。 的 数据的表包含如电话号码,邮件ID,地址等所有联系信息。 在的原料接触的点创建的实际接触。因此,我们使用的原料接触的,同时增加了接触。 用户不能在联系人表中添加任何数据。此表中的数据在内部填充由于aggregation联系人中。 您的逻辑工作过一段接触的原因是:_ID对于的联系人的 原料接触的遗体同样的,直到有任何接触聚集发生。比方说,你添加具有相同名称的美国广播公司的两个触点。在这里,_ID对于的原料接触的增量的两倍,而_ID对于的联系人的增量只有一次因为这两个触点被合并,由于到aggregation联系人 The Contacts database is divided into 3 tables contacts, raw contacts, and data. Each table contains column (_ID) which is an auto incremented primary key. data table contains all the contact info like phone number, mail id, address etc. The raw contacts points to the actual contact created. Hence we use the raw contacts while adding a contact. The user cannot add any data in the contacts table. The data in this table is populated internally due to aggregation of contacts. 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

了解更多详情。

抓取你的情况的信息,最好的方法是使用 ContactsContract.RawContactsEntity (该raw_contacts表与数据表中的外连接)

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)

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