简体   繁体   中英

Modifying starred contacts

I need (for practicing reasons) to change all the contacts to be starred. So I use this code to read all the contacts in a thread:

Looper.prepare(); //To avoid error: Can't create handler inside thread that has not called Looper.prepare
CursorLoader oCursorLoader = new CursorLoader(ContextoGlobal, ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
Cursor oCursor = oCursorLoader.loadInBackground();

int contactId = oCursor.getColumnIndex(ContactsContract.Contacts._ID);
contactId = oCursor.getColumnIndex(ContactsContract.RawContacts._ID);
int starred = oCursor.getColumnIndex(ContactsContract.Contacts.STARRED);
int number = oCursor.getColumnIndex(ContactsContract.Contacts.Data.DATA1);
int name = oCursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);

oCursor.moveToFirst();
if(oCursor.isAfterLast()==false) {
    do {
        String sId = oCursor.getString(contactId);
        String phNumber = oCursor.getString(number);
        String phName = oCursor.getString(name);
        String sStarred = oCursor.getString(starred);
        String s = sId + "\n" + phName + "\n" + phNumber + "\nStarred: " + sStarred;      

    } while (oCursor.moveToNext());
}

This code works and iterates through all the contacts in the device, displaying if they are starred or not.

My problem comes when I want to modify the starred field in the loop:

...
do {
    String sId = oCursor.getString(contactId);
    String phNumber = oCursor.getString(number);
    String phName = oCursor.getString(name);
    String sStarred = oCursor.getString(starred);
    String s = sId + "\n" + phName + "\n" + phNumber + "\nStarred: " + sStarred;

    ChangeStarred(sId, true);  <-- HERE!!!!!!!!

} while (oCursor.moveToNext());
...

This is the ChangeStarred() function:

private boolean ChangeStarred(String sContactId, boolean bStarred){
    ContentValues values = new ContentValues();
    if(bStarred==true)
        values.put(ContactsContract.Contacts.STARRED, 1);
    else
        values.put(ContactsContract.Contacts.STARRED, 0);

    //int iAffectedRows = ContextoGlobal.getContentResolver().update(ContactsContract.Contacts.CONTENT_URI, values, ContactsContract.Contacts._ID + "= ?", new String[] { sContactId });
    int iAffectedRows = ContextoGlobal.getContentResolver().update(ContactsContract.Contacts.CONTENT_URI, values, ContactsContract.RawContacts._ID + "= ?", new String[] { sContactId });

    if(iAffectedRows == 0)
        return false;
    return true;
}

This function always returns FALSE. No rows are updated. As you can see in the code comments, I have tried with Contacts._ID and RawContacts._ID

I also have WRITE_CONTACTS permission granted.

This is how I solved:

Looper.prepare(); //To avoid error: Can't create handler inside thread that has not called Looper.prepare
CursorLoader oCursorLoader = new CursorLoader(ContextoGlobal, ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
Cursor oCursor = oCursorLoader.loadInBackground();

int contactId = oCursor.getColumnIndex(ContactsContract.Contacts._ID);
int starred = oCursor.getColumnIndex(ContactsContract.Contacts.STARRED);
int name = oCursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);

oCursor.moveToFirst();
if(oCursor.isAfterLast()==false) {
    do {
        String sId = oCursor.getString(contactId);
        String phName = oCursor.getString(name);
        String sStarred = oCursor.getString(starred);
        String s = sId + "\n" + phName + "\n" + "\nStarred: " + sStarred;     

        ChangeStarred(sId, true);
    } while (oCursor.moveToNext());
}

And the ChangeStarred() function:

private boolean ChangeStarred(String sContactId, boolean bStarred) {
    ContentValues contentValues = new ContentValues();

    if(bStarred==true)
        contentValues.put(ContactsContract.Contacts.STARRED, 1);
    else
        contentValues.put(ContactsContract.Contacts.STARRED, 0);

    int iAffectedRows = ContextoGlobal.getContentResolver().update(ContactsContract.Contacts.CONTENT_URI, contentValues, ContactsContract.Contacts._ID + "=" + sContactId, null);

    if(iAffectedRows > 0)
        return true;
    return false;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM