我想将我的联系人从 listview 添加到 android 中的联系人页面。我可以添加联系人。但是如何避免重复呢?
例如:我有一个名为 Jony 和编号为 123 的条目。如果我再次按下同一个联系人,则不应将其添加到联系人页面。如果联系人已经在联系人页面中,我不想添加联系人。我怎样才能做到这一点?
这是我的代码:
import_btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ArrayList<HashMap<String, String>> newArrayList=null;
//ArrayList<HashMap<String, String>> selectedContact=null;
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
newArrayList=ContactImportAdapter.contactsArrayList;
for(int i=0;i<newArrayList.size();i++)
{
String name;
String number;
String mail;
name=newArrayList.get(i).get("import_viewContactName");
number=newArrayList.get(i).get("import_viewContactNumber");
mail=newArrayList.get(i).get("import_viewContactMail");
int rawContactInsertIndex = ops.size();
ops.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI)
.withValue(RawContacts.ACCOUNT_TYPE, null)
.withValue(RawContacts.ACCOUNT_NAME, null).build());
ops.add(ContentProviderOperation
.newInsert(Data.CONTENT_URI)
.withValueBackReference(Data.RAW_CONTACT_ID,rawContactInsertIndex)
.withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE)
.withValue(StructuredName.DISPLAY_NAME, name) // Name of the person
.build());
ops.add(ContentProviderOperation
.newInsert(Data.CONTENT_URI)
.withValueBackReference(
ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex)
.withValue(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE)
.withValue(Phone.NUMBER, number) // Number of the person
.withValue(Phone.TYPE, Phone.TYPE_MOBILE).build());
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex)
.withValue(ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Email.DATA, mail)
.withValue(ContactsContract.CommonDataKinds.Email.TYPE, ContactsContract.CommonDataKinds.Email.TYPE_WORK)
.build());
//Toast.makeText(getApplicationContext(), "Contact Added Successfully", Toast.LENGTH_SHORT).show();
}
newArrayList.clear();
try
{
ContentProviderResult[] res = getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
}
catch (RemoteException e)
{
// error
}
catch (OperationApplicationException e)
{
// error
}
}
});