【问题标题】:updating phone contact not working更新电话联系人不起作用
【发布时间】:2014-05-16 13:32:34
【问题描述】:

我正在尝试提取我的联系人并对其应用更改,但问题是我的构建器似乎工作正常并且没有抛出任何异常,但没有任何更改!

Cursor c;
    ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();

    Builder builder = ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI);
    builder.withSelection(ContactsContract.Data.CONTACT_ID + "=?" + " AND " + ContactsContract.Data.MIMETYPE + "=?", new String[]{String.valueOf(ContactID), ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE});
    builder.withValue(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,"blabla");
    ops.add(builder.build());

    ContentProviderResult[] res;
    try
    {
        res = getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
                        }
    catch (Exception e)
    {
        e.printStackTrace();
    }
    setListViewAdapter() ;
    c = getContentResolver().query(CommonDataKinds.Phone.CONTENT_URI, null, CommonDataKinds.Phone.CONTACT_ID +" = ?", new String[]{ContactID}, null);
    try {
           c.moveToFirst();
           displayName = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
        } 
    finally
        {
                c.close();
        }

这是我将适配器更新为联系人列表视图的方法

private void setListViewAdapter() {

    // Construct the data source
    arrayOfUsers = getContactsData();


    // update the adapter to convert the array to views
    adapter.clear();
    adapter.addAll(arrayOfUsers);
    adapter.notifyDataSetChanged(); 


    // Attach the adapter to a ListView
    if (adapter!=null && lstContacts!=null)
     {
        try
        {
            lstContacts.setAdapter(adapter);
        }
        catch(Exception e) {
            System.out.println("Error! "+e.getMessage());
        }
    }
     else
        NoContacts.setVisibility(View.VISIBLE);
}

这就是我从电话联系人目录中获取联系人信息的方式,效果很好。

private ArrayList<Contact> getContactsData() {
    String ID   = ""; 
    String name   = ""; 
    String number = ""; 

    ArrayList <Contact> mycontacts = null;
    try
    {
        //get our contact list via a Content Resolver which is the bridge between all android applications
        Uri mContacts_uri= ContactsContract.Contacts.CONTENT_URI;

        people = getContentResolver().query(mContacts_uri, // Contact URI
                                            null,    // Which columns to return
                                            null,       // Which rows to return
                                            null,       // Where clause parameters
                                            null        // Order by clause
                                            );
        int indexID = people.getColumnIndex(ContactsContract.Contacts._ID);
        int indexName = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
        int indexhasnumbers = people.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER);

        mycontacts = new ArrayList<Contact>();
        Contact _person = null;
        int cur = people.getCount();
        if(cur>0)
        {
            people.moveToFirst();
            do {
            ID   = people.getString(indexID);
            name   = people.getString(indexName);

            /********************************************/
            if (Integer.parseInt(people.getString(indexhasnumbers)) > 0) 
            {
                 Cursor pCur = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
                                         null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", 
                                         new String[]{ID}, null);
                 try{

                 while (pCur.moveToNext()) {
                      int phoneType = pCur.getInt(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
                      number = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                      switch (phoneType) {
                            case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE:
                                Log.e(name + "(mobile number)", number);
                                break;
                            case ContactsContract.CommonDataKinds.Phone.TYPE_HOME:
                                Log.e(name + "(home number)", number);
                                break;
                            case ContactsContract.CommonDataKinds.Phone.TYPE_WORK:
                                Log.e(name + "(work number)", number);
                                break;
                            case ContactsContract.CommonDataKinds.Phone.TYPE_OTHER:
                                Log.e(name + "(other number)", number);
                                break;                                  
                            default:
                                break;
                      }
                  } 
                  pCur.close();
                }
                 catch(Exception e)
                 {
                     Log.e("exception found : "+ e.getMessage()," : phone number ");
                 }
            _person=new Contact(ID,name,number);
            mycontacts.add(_person);
            }
        } 
        while (people.moveToNext());
        }

     }
    catch(Exception e) {
        System.out.println("Error! "+e.getMessage());
    }
     return mycontacts;     
}

但显示名称还是一样,我不明白更新功能的哪个部分不起作用,因为没有抛出异常

【问题讨论】:

  • 检查清单文件中的联系人数据库权限。
  • 我已经在使用读写权限了:

标签: android android-contentprovider contactscontract android-contentresolver android-cursor


【解决方案1】:

使用这个查询

String select = "((" + Contacts.DISPLAY_NAME + " NOTNULL) AND ("
                    + Contacts.HAS_PHONE_NUMBER + "=1) AND ("
                    + Contacts.DISPLAY_NAME + " != '' ))";

            Cursor c = cntx.getContentResolver().query(Contacts.CONTENT_URI, CONTACTS_SUMMARY_PROJECTION, select,
                    null, Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");

查看此链接一次可能会对您有所帮助

http://developer.android.com/reference/android/provider/ContactsContract.RawContacts.html

【讨论】:

  • 我不明白,现在我更困惑了,我只是在你给我的链接中尝试了插入方法,仍然没有更新我的联系人!顺便说一句,我在 manifest.xml 中使用了 READ 和 WRITE 权限!
  • 使用我的查询你没有得到所有的联系人
  • 我做到了,问题是更新没有得到联系人
  • 所以意思是从我的查询中说你也无法获得更新的查询
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多