Android 如何使用内容提供商更新现有联系人
我正在尝试使用内容提供商编辑我手机的联系人。为了加载数据,我使用了下面的代码,效果很好Android 如何使用内容提供商更新现有联系人,android,android-contentprovider,Android,Android Contentprovider,我正在尝试使用内容提供商编辑我手机的联系人。为了加载数据,我使用了下面的代码,效果很好 private ArrayList<String> getRecords() { ArrayList<String> records=new ArrayList<String>(); Cursor cursor=getContentResolver().query( ContactsContract.CommonDataKinds.Phone.
private ArrayList<String> getRecords()
{
ArrayList<String> records=new ArrayList<String>();
Cursor cursor=getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,null, null);
if (cursor.moveToFirst())
{
String name="";
String phone="";
String id="";
do{
id=cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone._ID));
name =cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
phone =cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
name = id+" "+name+"\n"+phone;
records.add(name);
}
while(cursor.moveToNext());
}
return records;
}
private ArrayList getRecords()
{
ArrayList记录=新的ArrayList();
Cursor Cursor=getContentResolver().query(
ContactsContract.CommonDataTypes.Phone.CONTENT\u URI,null,null,null,null);
if(cursor.moveToFirst())
{
字符串名称=”;
字符串phone=“”;
字符串id=“”;
做{
id=cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataTypes.Phone.\u id));
name=cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataTypes.Phone.DISPLAY_name));
phone=cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataTypes.phone.NUMBER));
name=id+“”+name+“\n”+电话;
记录。添加(名称);
}
while(cursor.moveToNext());
}
退货记录;
}
现在我想编辑,实际上我想更改所选联系人的姓名。我正在尝试下面的代码
Uri uri= ContentUris.withAppendedId(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, id);
ContentValues values=new ContentValues();
values.put(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, "<r XX");
getContentResolver().update(uri, values, null,null);
uriuri=ContentUris.withAppendedId(ContactsContract.commondatatypes.Phone.CONTENT\u Uri,id);
ContentValues=新的ContentValues();
values.put(contacts contract.commonDataTypes.Phone.DISPLAY_NAME,“您似乎没有正确提供更新参数:
该方法包括:
getContentResolver().update(uri, values, where, selectionArgs)
其中应包括:
"ContactsContract.CommonDataKinds.Phone._ID+"=?"
并且selectionArgs应该包含要更新的联系人的id。Uri Uri=ContentUris.withAppendedId(ContactsContract.CommonDataTypes.Phone.CONTENT_Uri,3625);ContentValues=new ContentValues();values.put(ContactsContract.CommonDataTypes.Phone.DISPLAY_NAME,“Mr XX”);getContentResolver().update(uri,值,ContactsContract.CommonDataTypes.Phone.\u ID+“=?”,新字符串[]{String.valueOf(ID)});检查此代码。但它不起作用:-(