我想将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
}
}
});
答案 0 :(得分:1)
首先为了避免重复,你必须首先调用数据库,如果存在数字则检查数字,然后你可以处理。