raki_android
Lurker
Hi Guys,
I am new to android, so thought of doing a small project on Native Contacts. Application should Add, Update & Delete Native Contacts through my application.
Adding & Deleting native contacts is working fine, but Update contacts is not working at all.
This is the code i am using for Add, Update & Delete :-
Add To Native :-
ContentResolver cr = getContentResolver();
Uri uri= People.CONTENT_URI;
int id = -1;
ContentValues values = new ContentValues();
Uri phoneUri = Uri.withAppendedPath(uri, People.Phones.CONTENT_DIRECTORY);
Uri email_address_Uri = Uri.withAppendedPath(uri, People.ContactMethods.CONTENT_DIRECTORY);
int len = 0;
Cursor mCursor;
values = new ContentValues();
values.put(People.NAME, result[0] + " " +result[1]+ " " + result[2]);
uri = cr.insert(People.CONTENT_URI, values);
// cr.update(email_address_Uri, values, res, result);
values.clear();
phoneUri = Uri.withAppendedPath(uri, People.Phones.CONTENT_DIRECTORY);
email_address_Uri = Uri.withAppendedPath(uri, People.ContactMethods.CONTENT_DIRECTORY);
Uri add = Uri.withAppendedPath(uri, People.ContactMethods.DATA);
mCursor = cr.query(uri, null, null, null, null);
mCursor.moveToLast();
id = mCursor.getInt(mCursor.getColumnIndex(People._ID));
mCursor.close();
if(result[6].trim().trim().length() > 0){
values.put(People.Phones.TYPE, People.Phones.TYPE_WORK);
values.put(People.Phones.NUMBER, result[6].trim());
cr.insert(phoneUri, values);
values.clear();
}
if(result[7].trim().trim().length() > 0){
values.put(People.Phones.TYPE, People.Phones.TYPE_HOME);
values.put(People.Phones.NUMBER, result[7].trim());
cr.insert(phoneUri, values);
values.clear();
}
if(result[8].trim().length() > 0){
values.put(People.Phones.TYPE, People.Phones.TYPE_MOBILE);
values.put(People.Phones.NUMBER, result[8].trim());
cr.insert(phoneUri, values);
values.clear();
}
if(result[4].trim().length() > 0){
values.put(People.ContactMethods.KIND, Contacts.KIND_POSTAL);
values.put(People.ContactMethods.DATA, result[4].trim() +"\n" + result[3].trim());
values.put(People.ContactMethods.TYPE, People.ContactMethods.TYPE_WORK);
cr.insert(email_address_Uri, values);
values.clear();
}
if(result[5].trim().length() > 0){
values.put(People.ContactMethods.KIND, Contacts.KIND_POSTAL);
values.put(People.ContactMethods.DATA, result[5].trim());
values.put(People.ContactMethods.TYPE, People.ContactMethods.TYPE_HOME);
cr.insert(email_address_Uri, values);
values.clear();
}
Delete :-
cr.delete(uri, People._ID + "=" + id, null);
Update :-
values.put("data1", "Student_Name");
cr.update(Uri.parse("content://com.android.contacts/data/"),
values, "raw_contact_id=" + id, null);
Suppose i am adding a new contact in the Native itself i.e without using with my AddtoNative method :-
Name : Robert
Home Phone : 111111111
Work Phone : 222222222
Mobile Phone: 333333333
Work Fax : 444444444
Home Email : robert@gmail.com
if i have use update code given below :-
values.put("data1", "Student_Name");
cr.update(Uri.parse("content://com.android.contacts/data/"),
values, "raw_contact_id=" + id, null);
All data gets updated and gives the result :-
Name : Student_Name
Home Phone : Student_Name
Work Phone : Student_Name
Mobile Phone: Student_Name
Work Fax : Student_Name
Home Email : Student_Name
But in update, i just want to only update Work phone number or Home Phone number or Home Email field.
Any suggestions guys.........
Thanks in Advance.
Regards
Raki
I am new to android, so thought of doing a small project on Native Contacts. Application should Add, Update & Delete Native Contacts through my application.
Adding & Deleting native contacts is working fine, but Update contacts is not working at all.
This is the code i am using for Add, Update & Delete :-
Add To Native :-
ContentResolver cr = getContentResolver();
Uri uri= People.CONTENT_URI;
int id = -1;
ContentValues values = new ContentValues();
Uri phoneUri = Uri.withAppendedPath(uri, People.Phones.CONTENT_DIRECTORY);
Uri email_address_Uri = Uri.withAppendedPath(uri, People.ContactMethods.CONTENT_DIRECTORY);
int len = 0;
Cursor mCursor;
values = new ContentValues();
values.put(People.NAME, result[0] + " " +result[1]+ " " + result[2]);
uri = cr.insert(People.CONTENT_URI, values);
// cr.update(email_address_Uri, values, res, result);
values.clear();
phoneUri = Uri.withAppendedPath(uri, People.Phones.CONTENT_DIRECTORY);
email_address_Uri = Uri.withAppendedPath(uri, People.ContactMethods.CONTENT_DIRECTORY);
Uri add = Uri.withAppendedPath(uri, People.ContactMethods.DATA);
mCursor = cr.query(uri, null, null, null, null);
mCursor.moveToLast();
id = mCursor.getInt(mCursor.getColumnIndex(People._ID));
mCursor.close();
if(result[6].trim().trim().length() > 0){
values.put(People.Phones.TYPE, People.Phones.TYPE_WORK);
values.put(People.Phones.NUMBER, result[6].trim());
cr.insert(phoneUri, values);
values.clear();
}
if(result[7].trim().trim().length() > 0){
values.put(People.Phones.TYPE, People.Phones.TYPE_HOME);
values.put(People.Phones.NUMBER, result[7].trim());
cr.insert(phoneUri, values);
values.clear();
}
if(result[8].trim().length() > 0){
values.put(People.Phones.TYPE, People.Phones.TYPE_MOBILE);
values.put(People.Phones.NUMBER, result[8].trim());
cr.insert(phoneUri, values);
values.clear();
}
if(result[4].trim().length() > 0){
values.put(People.ContactMethods.KIND, Contacts.KIND_POSTAL);
values.put(People.ContactMethods.DATA, result[4].trim() +"\n" + result[3].trim());
values.put(People.ContactMethods.TYPE, People.ContactMethods.TYPE_WORK);
cr.insert(email_address_Uri, values);
values.clear();
}
if(result[5].trim().length() > 0){
values.put(People.ContactMethods.KIND, Contacts.KIND_POSTAL);
values.put(People.ContactMethods.DATA, result[5].trim());
values.put(People.ContactMethods.TYPE, People.ContactMethods.TYPE_HOME);
cr.insert(email_address_Uri, values);
values.clear();
}
Delete :-
cr.delete(uri, People._ID + "=" + id, null);
Update :-
values.put("data1", "Student_Name");
cr.update(Uri.parse("content://com.android.contacts/data/"),
values, "raw_contact_id=" + id, null);
Suppose i am adding a new contact in the Native itself i.e without using with my AddtoNative method :-
Name : Robert
Home Phone : 111111111
Work Phone : 222222222
Mobile Phone: 333333333
Work Fax : 444444444
Home Email : robert@gmail.com
if i have use update code given below :-
values.put("data1", "Student_Name");
cr.update(Uri.parse("content://com.android.contacts/data/"),
values, "raw_contact_id=" + id, null);
All data gets updated and gives the result :-
Name : Student_Name
Home Phone : Student_Name
Work Phone : Student_Name
Mobile Phone: Student_Name
Work Fax : Student_Name
Home Email : Student_Name
But in update, i just want to only update Work phone number or Home Phone number or Home Email field.
Any suggestions guys.........
Thanks in Advance.
Regards
Raki
