Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to avoid duplication while saving contact in android
    primarykey
    data
    text
    <p>I want to add my contacts from listview to contact page in android.. I am able to add contacts. But how do I avoid duplicates? </p> <p>For example: I have an entry with name Jony and number 123. If I press that same contact again it shouldn't be added to contact page. I dont want to add the contact if it is already in the contact page. How can I do this?</p> <p>Here is my code:</p> <pre><code>import_btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub ArrayList&lt;HashMap&lt;String, String&gt;&gt; newArrayList=null; //ArrayList&lt;HashMap&lt;String, String&gt;&gt; selectedContact=null; ArrayList&lt;ContentProviderOperation&gt; ops = new ArrayList&lt;ContentProviderOperation&gt;(); newArrayList=ContactImportAdapter.contactsArrayList; for(int i=0;i&lt;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 } } }); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload