سؤال

أود تحديث التعليمات البرمجية الخاصة بي بيانات المتصل (مثل الاسم ورقم الهاتف والبريد الإلكتروني وتفاصيل المؤسسة وغيرها) في دفتر اتصال Android. كنت ناجحا في تعديل عدد قليل (الاسم ورقم الهاتف والبريد الإلكتروني لتكون محددة) ولكن ليس كل شيء.

كلما حاولت تحديث تفاصيل المنظمة (جهات الاتصال .organizations.company وجهات الاتصال .organizations.title) لاتصال التطبيق الخاص بي يلقي استثناء

java.lang.UnsupportedOperationException: Cannot update URL: content://contacts/people/69/organizations/69

مقتطف التعليمات البرمجية كما يلي:

Uri baseUri = ContentUris.withAppendedId(People.CONTENT_URI, 69);
Uri uri = Uri.withAppendedPath(baseUri, People.Phones.CONTENT_DIRECTORY);
Cursor c = this.getContentResolver().query(uri, 
                new String[] { Contacts.Organizations._ID, Contacts.Organizations.COMPANY,Contacts.Organizations.TITLE}, 
                null, null, null);
if(c.getCount() > 0) {
      uri = ContentUris.withAppendedId(uri, c.getString(0));
ContentValues val1 = new ContentValues();
val1.put(Contacts.Organizations.COMPANY, "arw");
val1.put(Contacts.Organizations.TYPE, Contacts.Organizations.TYPE_WORK);
val1.put(Contacts.Organizations.TITLE, "abcdef");
this.getContentResolver().insert(uri, val1);
هل كانت مفيدة؟

المحلول

من أجل تحديث أي بيانات في جهات الاتصال، تحتاج إلى معرفة المعرف الحالي لهذه الحقول. ثم استخدمت فئة Builder للحصول على كائنات منفصلة ContentProvideroperoperoperation لمختلف الحقول التي أردت تحديثها، وأضفها إلى قائمة صفيف ثم استخدم طريقة Contentprovider.applybatch ()

نصائح أخرى

قضيت الكثير من الوقت في محاولة تحديث معلومات الشركة والملكية. ما يلي يعمل الآن بالنسبة لي.

Uri workUri = Uri.withAppendedPath(contactUri, android.provider.Contacts.Organizations.CONTENT_DIRECTORY);
            values.clear();
            values.put(android.provider.Contacts.Organizations.COMPANY, "company");
            values.put(android.provider.Contacts.Organizations.TYPE, android.provider.Contacts.Organizations.TYPE_WORK);
            values.put(android.provider.Contacts.Organizations.TITLE, "job title");
            values.put(android.provider.Contacts.Organizations.ISPRIMARY, 1);
            getContentResolver().insert(workUri, values); 
String[] projection = new String[] { ContactsContract.RawContacts._ID };
        String where = ContactsContract.RawContacts.CONTACT_ID + " = ?";
        String[] selection = new String[] { String.valueOf(1) };
        Cursor c = getContentResolver().query(ContactsContract.RawContacts.CONTENT_URI, projection, where, selection, null);
        c.moveToFirst();
        for(int i=0;i<c.getCount();i++)
        {
            Log.e("id",""+c.getString(0));
            c.moveToNext();
        }



        ContentValues values = new ContentValues();
        values.clear();
        values.put(ContactsContract.RawContacts._ID , 0);
        getContentResolver().update(ContactsContract.RawContacts.CONTENT_URI, values,ContactsContract.RawContacts._ID + "+ ?",new String[] { String.valueOf(1) });
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top