jeudi 17 décembre 2015

Contacts are saved multiple times in Database

Hi guys i have code to add data into Database and that code is called on oncreate. but every time fragment is created its saves data into database again and again. I dont want that. how can i prevent that here is my code to get data and save it to database.

public void getcontacts(){
    Cursor phones;
    phones = getActivity().getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
    while (phones.moveToNext()){
        String name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
        String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
        DataBaseOperations hell = new DataBaseOperations(getActivity());
        SQLiteDatabase db = hell.getWritableDatabase();
        hell.SaveContacts(name, phoneNumber, db);
    }
    phones.close();
}

here is code for saving in database.

public void SaveContacts(String Name,String phone,SQLiteDatabase db ){
    ContentValues sv = new ContentValues();
    sv.put(mDatabase.Tableinfo.Contacts_name, Name);
    sv.put(mDatabase.Tableinfo.Contacts_phone, phone);
    db.insert(mDatabase.Tableinfo.contacts, null, sv);
}

Aucun commentaire:

Enregistrer un commentaire