本人菜鸟,最近在写一个简易通讯录,但是在写删除功能的时候遇到了问题,下面是部分代码
OnItemClickListener listener = new OnItemClickListener() {
public void onItemClick(final AdapterView<?> parent, View view, final int position,
long id) {
//setTitle(parent.getItemAtPosition(position).toString());
Builder builder = new AlertDialog.Builder(MainActivity.this);
final String[] items = new String[] {"拨打","修改","删除"};
builder.setItems(items,new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if(which == 0){
HashMap ob=(HashMap)parent.getItemAtPosition(position);
String number = ob.get("number").toString();
Intent intent = new Intent();
intent.setAction(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" + number));
startActivity(intent);
}
if(which == 1){
HashMap ob=(HashMap)parent.getItemAtPosition(position);
String name =ob.get("name").toString();
String number = ob.get("number").toString();
Intent int_edit=new Intent(MainActivity.this,EditActivity.class);
int_edit.putExtra("opt",2);
int_edit.putExtra("name", name);
int_edit.putExtra("number",number);
int_edit.putExtra("no",data.indexOf(ob));
startActivityForResult(int_edit,REQUEST_EDITACTIVITY);
}
if(which == 2)
{
data.remove(position);
adapter.notifyDataSetChanged();
ContentResolver cr = getContentResolver();
Cursor contactsCur = cr.query(ContactsContract.RawContacts.CONTENT_URI, null, null, null, null);
while(contactsCur.moveToNext()){
String Id = contactsCur.getString(contactsCur.getColumnIndex(RawContacts._ID));
getContentResolver().delete(RawContacts.CONTENT_URI,ContactsContract.RawContacts._ID + "=" + Id,null);
}
}
}
});
builder.create().show();
}
};
我用一个listview显示联系人,然后点击一个条目就出现三个选项,第一个是拨打,第二个是修改,这两个都没问题,关键就是第三个删除,上面的代码能做到把通讯录里的联系人全部删除,但是我不知道如何做到删除指定的联系人,请问有什么方法能实现呢?希望大神解答,谢谢!
------解决方案--------------------
把系统的contacts数据库到出来好好研究就知道了,删除联系人要删除两个表的信息,