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();
}
}
}
}
Aucun commentaire:
Enregistrer un commentaire