我正在尝试让我的应用更新一些联系人,但它正在更新错误的人。我得到联系人姓名,号码和原始身份证。然后,如果我改变他的号码并通过他的原始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();
}
}
我做错了什么?
答案 0 :(得分:0)
我认为错误的是您在获取数字时检索RAW_CONTACT_ID,但在update()中,您将此值传递给CONTACT_ID列。第一个指向原始联系人,但第二个指向联系人表。不要混合它们!
我还注意到您正在尝试使用Phone.TYPE的值更新ORGANIZATION列,这可能有效,也可能无效。