-1

I'm tryingt ot extract my contacts and apply so changes on them but the problem is that my builder seems like it's working fine and not throwing any exception but there are no changes !

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

and here's my method to Update the adapter to a ListView of contacts

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

and this is how I get contacts info from the phone contacts directory, which works fine.

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

but the display name still the same, I don't understand which part of the updating function is not working because none is throwing an exception

Joy
  • 1,707
  • 7
  • 29
  • 44

1 Answers1

0

use this query

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

see this link once may this will help you

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

Bhanu Sharma
  • 5,135
  • 2
  • 24
  • 49