0

アプリで一部の連絡先を更新しようとしていますが、間違った人を更新しています。連絡先の名前、番号、Raw ID を取得します。次に、私が彼の番号を変更し、彼の Raw ID で更新した場合、それが私の真実である場合。それが私のコードです:

Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
while (phones.moveToNext())
{
    String numero;
    String fnum;
    String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
    int id = phones.getInt(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.RAW_CONTACT_ID));
    String tipo = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
    phoneNumber = phoneNumber.replace(" ", "");
    phoneNumber = phoneNumber.replace("-", "");
    phoneNumber = phoneNumber.replace("+", "");
    phoneNumber = phoneNumber.replace("*", "");
    phoneNumber = phoneNumber.replace("#", "");

    int tamanho = phoneNumber.length();
    numero = phoneNumber;

    if (tamanho == 12) {
        if (checar.indexOf(numero.substring(1, 3) + ",") != -1) {
            if(numero.substring(3).startsWith("9")){
                fnum = numero.substring(0, 3) + "" + numero.substring(4);
                update(id, fnum, tipo);
            }
        }
    }
    Thread.sleep(2000);
}
phones.close();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

そして、それが更新方法です:

public void update(int id, String number, String tipo)
    {

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

        // Number
        builder = ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI);
        builder.withSelection(ContactsContract.Data.CONTACT_ID + "=?" + " AND " + ContactsContract.Data.MIMETYPE + "=?"+ " AND " + ContactsContract.CommonDataKinds.Organization.TYPE + "=?", new String[]{String.valueOf(id), ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE, tipo});
        builder.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, number);
        ops.add(builder.build());

        // Update
        try
        {
            getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

私は何を間違っていますか?

4

1 に答える 1