1

I am creating SQLite Database for my Mobile App. The App Database will store copy of contacts that are stored in Android Phones. Below code is used to copy contacts from Database. My problem is that every time App is opened , it copies entire data into SQLite Database that has resulted lot of duplicate entry. If I try to check existence of entries before copying the records into SQLite database then every time when App is launched, it will be checking of duplicity resulting overloading on application.

In order to solve the problem I thought if any response comes after successful addition of contact by user, then we can use this response to add contacts in SQLite database and then we can take out Database copying on launch of App. As I am new into the world of Android, requesting you to advice best way to tackle this problem.

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button prd = (Button) findViewById(R.id.duct);
    prd.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            load_contacts();
            display_cotacts();
            Log.i("Activity Main", "Completed Displaying List");
        }
    });
}
private void load_contacts(){
    openDB();
    ContentResolver cr = getContentResolver();
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,null,null,null,null);
    //String[] from = {ContactsContract.Phone.DISPLAY_NAME,ContactsContract.CommonDataKinds.Phone.NUMBER,ContactsContract.CommonDataKinds.Phone_ID};
    if(cur.getCount()>0) {
        while(cur.moveToNext()){
            String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
            String name = cur.getString((cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)));
            if(Integer.parseInt(cur.getString(
                    cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) >0){
                Cursor pCur = cr.query(
                        ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                        null,
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
                        new String[]{id},null);
                while(pCur.moveToNext()){
                    Integer mobile = pCur.getInt(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    String email = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.ADDRESS));
                    Integer imageID = pCur.getInt(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Photo.PHOTO_FILE_ID));
                    String status = "available";
                    String time = "now";
                    long newId = myDb.insertRow(name,mobile,email,imageID,status,time);
                    Toast.makeText(MainActivity.this, "Name: " + name + "Phone no:" + mobile + "Email:" + email + "image:" + imageID + "image_thumbnail:" + status, Toast.LENGTH_LONG).show();
                }
                //closeDB();
                pCur.close();
            }
        }

    }

}
Sebastian Walla
  • 1,104
  • 1
  • 9
  • 23
Sukesh Saxena
  • 201
  • 2
  • 18
  • 2
    Couldn't you use the `CONTACT_ID` or `RAW_CONTACT_ID` as a primary key or a unique key constrain for your database ? thus blocking any insertion of duplicate ? – dvhh Sep 01 '15 at 08:05
  • If you use a CursorLoader, it should receive an update to it whenever the data is changed. If the Contacts provider uses notifyUri. It may not. However, if it does, you can use CursorLoader to load the Contacts provider and be informed when the Contacts are changed (you can probably even put that into a Service). – Knossos Sep 01 '15 at 09:08
  • Thnx Knossos for your help. Is it possible to provide few line of code for better understanding your above statement. Thnx in advance for your help. – Sukesh Saxena Sep 01 '15 at 10:15
  • Check out this link, came in handy for me. http://stackoverflow.com/questions/19829803/how-to-remove-duplicate-contact-from-contact-list-in-android – Michael Mburu May 27 '16 at 20:11

1 Answers1

0

I'd suggest to use INSERT OR IGNORE INTO exampletable (UserId, UserName, ContactName) VALUES('1','UserName','ContactName'); // the variables are just examples;

or INSERT OR REPLACE INTO exampletable (UserId, UserName, ContactName) VALUES('1','UserName','ContactName');

Replace will update the existing entry with the new variable values and ignore is pretty self-explanatory.

This would fix your problem of doubled entrys and the checking of this would be done by your sqlite-database.

For checking the last modification date you should have a look at this stackoverflow answer.

You could simply retrieve this date and check wether it differs with the last time the contact was modified. But this will also be done for every contact, so not a perfect solution at all.

Sebastian Walla
  • 1,104
  • 1
  • 9
  • 23
  • Thnx for the Ans. This will help for checking duplicacy but will scan SQLite on every time App will be launched even though there is no new contacts get added by User. I am looking for ways in which any updates in Phone's contact book can also update SQLite. This will avoid unnecessary looking up SQLite on launch of activity. Hope I made by point clear. Please let me know. – Sukesh Saxena Sep 01 '15 at 08:29
  • But I´d recommend to check the changes in a `BackgroundActivity` to avoid overusing the Main-Thread. – Sebastian Walla Sep 01 '15 at 15:56