修改已加星标的联系人

问题描述 投票:0回答:1

我需要(出于练习原因)更改所有要加星标的联系人。所以我用这段代码来读取线程中的所有联系人:

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

此函数始终返回FALSE。没有行更新。正如您在代码注释中看到的,我尝试过使用Contacts._ID和RawContacts._ID

我也获得了WRITE_CONTACTS权限。

android contacts
1个回答
0
投票

这就是我解决的问题:

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;
}
© www.soinside.com 2019 - 2024. All rights reserved.