我想从android数据库中删除(例如手机号码).为此,我传递的查询如下
public class ContactDemo extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String number = "2222";
Long id = getID(number);
int i = getContentResolver().delete(RawContacts.CONTENT_URI, RawContacts._ID+"=?", new String[]{id.toString()});
System.out.println("Deleted"+i);
}
public Long getID(String number){
Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
Cursor c = getContentResolver().query(uri, new String[]{PhoneLookup._ID}, null, null, null);
while(c.moveToNext()){
return c.getLong(c.getColumnIndex(PhoneLookup._ID));
}
return null;
}
}
但它正在删除整个联系人.
我应该使用什么来删除该电话号码(不是整个联系人)?
解决方法:
您使用ContentResolver的删除方法,以便删除整个联系人.要将此联系人的电话号码更新为空值,您需要使用ContactsContact API.
通过提供联系人原始ID和Phone.CONTENT_ITEM_TYPE,您只能请求属于此联系人的电话号码,然后删除所有这些电话号码.
ArrayList ops = Lists.newArrayList();
ops.add(ContentProviderOperation.newDelete(Data.CONTENT_URI)
.withSelection(Data._ID + "=? and " + Data.MIMETYPE + "=?", new String[]{String.valueOf(contactId), Phone.CONTENT_ITEM_TYPE})
.build());
getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
标签:android,cursor,contacts
来源: https://codeday.me/bug/20190531/1188714.html