Изменение помеченных контактов

Мне нужно (по практическим причинам) изменить все контакты, чтобы они были помечены звездочкой. Поэтому я использую этот код для чтения всех контактов в потоке:

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

Этот код работает и перебирает все контакты на устройстве, показывая, помечены они звездочкой или нет.

Моя проблема возникает, когда я хочу изменить поле со звездочкой в ​​цикле:

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

Это функция ChangeStarred():

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

Эта функция всегда возвращает ЛОЖЬ. Ни одна строка не обновляется. Как вы можете видеть в комментариях к коду, я пытался использовать Contacts._ID и RawContacts._ID.

У меня также есть разрешение WRITE_CONTACTS.

0
0
73
1
Перейти к ответу Данный вопрос помечен как решенный

Ответы 1

Ответ принят как подходящий

Вот как я решил:

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

И функция ChangeStarred():

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

Другие вопросы по теме