How do I update a email address from specific Email._id? - android

I have this problem .
I have a listview that is created depending on how many emails address have a contact .
Now I wish I could change this email address in a specific way .
If for example one of my contact has four email address ( one#gmail.com,
two#gmail.com,three#gmail.com,four#gmail.com )and modify one of these emails address for the second example ( two#yahoo.com ) how can I change this only leaving the other ?
For every email address I have a bean that includes : Email._id , Email.Type , Email.Data .
Thank you all for the help .
P.S.
Sorry for my English ;)
The problem is not in the listview , but in a specific point of contact email address .
This is the code that I used when I was a single email address to edit.
String emailData = "example#email.com";
String contactId="123456";
int typeEmail=1;
ops.add(ContentProviderOperation
.newUpdate(ContactsContract.Data.CONTENT_URI)
.withSelection(
ContactsContract.Data.CONTACT_ID + "=? AND " + ContactsContract.Data.MIMETYPE
+ "=?",
new String[] { contactId, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE })
.withValue(ContactsContract.CommonDataKinds.Email.DATA, emailData)
.withValue(ContactsContract.CommonDataKinds.Email.TYPE, typeEmail).build());
I solved it :
I simply changed the column to ContactsContract.Data.CONTACT_ID to ContactsContract.Data._ID
for (int i = 0; i < listEmail.size(); i++) {
ops.add(ContentProviderOperation
.newUpdate(ContactsContract.Data.CONTENT_URI)
.withSelection(
ContactsContract.Data._ID + "=? AND "
+ COLUMN_MIMETYPE + "=?",
new String[] { listEmail.get(i).getId(),
MIMETYPE_STRING_EMAIL })
.withValue(COLUMN_EMAIL, listEmail.get(i).getName())
.withValue(COLUMN_EMAIL_TYPE,
listEmail.get(i).getType()).build());
}

Related

Android Not able to update contact details programatically in local phonebook

I am having issue where i need to have update Multiple Mobile no's , Landline no's and Emails , websites , address in local phonebook contact.
If contact is already exist in phone book then i am trying to update it's details.
Below is the code i am trying. It is getting executed without error but no multiple mobile no , landline , email etc not reflecting in phone-book on that contact as the multiple data's are their.
I also referred few of the links but that didn't helped as well.
String whereMobile = ContactsContract.Data.RAW_CONTACT_ID + " = ? AND " +
ContactsContract.Data.MIMETYPE + " = ? AND " +
ContactsContract.CommonDataKinds.Phone.TYPE + " = ? ";
String[] paramsMobile = new String[]{String.valueOf(contactID),
ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE,
String.valueOf(ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE)};
if (userInfoMobileNos != null && !userInfoMobileNos.isEmpty()) {
for (int iUserMobile = 0; iUserMobile < userInfoMobileNos.size(); iUserMobile++) {
operationList.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
.withSelection(whereMobile, paramsMobile)
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, userInfoMobileNos.get(iUserMobile).getPhoneNumber())
.build());
}
}
String whereGmail = ContactsContract.Data.RAW_CONTACT_ID + " = ? AND " +
ContactsContract.Data.MIMETYPE + " = ? AND " +
ContactsContract.CommonDataKinds.Email.TYPE + " = ? ";
String[] paramsGmail = new String[]{String.valueOf(contactID),
ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE,
String.valueOf(ContactsContract.CommonDataKinds.Email.TYPE_WORK)};
if (userInfoGmails != null && !userInfoGmails.isEmpty()) {
for (int iUserGmail = 0; iUserGmail < userInfoGmails.size(); iUserGmail++) {
operationList.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
.withSelection(whereGmail, paramsGmail)
.withValue(ContactsContract.CommonDataKinds.Email.DATA, userInfoGmails.get(iUserGmail).getEmail())
.build());
}
}
Updated code
private void updateNew(Context context, int rawContactID) {
ArrayList<ContentProviderOperation> operationList = new ArrayList<>();
String whereMobile = ContactsContract.Data.RAW_CONTACT_ID + " = ? AND " +
ContactsContract.Data.MIMETYPE + " = ? AND " +
ContactsContract.CommonDataKinds.Phone.TYPE + " = ? ";
String[] paramsMobile = new String[]{String.valueOf(rawContactID),
ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE,
String.valueOf(ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE)};
// First delete all the existing phones with type mobile, if any exist
operationList.add(ContentProviderOperation.newDelete(ContactsContract.Data.CONTENT_URI)
.withSelection(whereMobile, paramsMobile)
.build());
// Next, insert all the updated phones:
for (MobileNumbersItem infoMobileNo : userInfoMobileNos) { // <== change the class to the one you're using in userInfoMobileNos
operationList.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) // <== insert not update!
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactID)
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE)
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, infoMobileNo.getPhoneNumber())
.build());
}
String whereGmail = ContactsContract.Data.RAW_CONTACT_ID + " = ? AND " +
ContactsContract.Data.MIMETYPE + " = ? AND " +
ContactsContract.CommonDataKinds.Email.TYPE + " = ? ";
String[] paramsGmail = new String[]{String.valueOf(rawContactID),
ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE,
String.valueOf(ContactsContract.CommonDataKinds.Email.TYPE_WORK)};
// First delete all the existing emails with type work, if any exist
operationList.add(ContentProviderOperation.newDelete(ContactsContract.Data.CONTENT_URI)
.withSelection(whereGmail, paramsGmail)
.build());
// Next, insert all the updated emails:
for (GmailsItem userInfoGmail : userInfoGmails) { // <== change the class here too
operationList.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactID)
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Email.TYPE, ContactsContract.CommonDataKinds.Email.TYPE_WORK)
.withValue(ContactsContract.CommonDataKinds.Email.DATA, userInfoGmail.getEmail())
.build());
}
try {
// don't forget to apply the operations now:
ContentProviderResult[] results = context.getContentResolver().applyBatch(ContactsContract.AUTHORITY, operationList);
Log.d("UPDATE CONTACRT", "results=" + Arrays.toString(results));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.e("updateContactError", Objects.requireNonNull(e.getMessage()));
}
}
Any help will be appreciated here. I have tried to debug but can't able to find the issue.
one issue that can be either a bad parameter name, or an actual bug is that your selection is requesting a RAW_CONTACT_ID and supplying a parameter called contactId, I'm not sure what is stored in contactId but there's a big difference between a rawContactId and a contactId, so if that's indeed a contactId your operations will not find anything to update (or even worse, update the wrong contact).
The second issue is that your two loops (over userInfoMobileNos and userInfoGmails) keep updating (overriding) the same Data row, so eventually you should end up with just the last value in the contact details.
You're also not checking if there's a Data row in the contact details that fit your selection, if there's not, there will be no row to update, and therefore the code will simply do nothing.
Third issue, but it may be intentional, not sure how you expect your app to work, is that you assume the info item type, i.e. you only update a phone if it's TYPE_MOBILE, and you only update the email if it's TYPE_WORK, what if there are other phones and emails stored for that contact? your code will simply keep those untouched, not sure if that intentional or not.
To fix issue one, just make sure the value in contactId is a RawContactId and change the name of the param.
For the second issue, you should instead do a delete-and-insert, see code below.
For the third issue, you'll need to change the selection code (paramsMobile) to remove it's assumption of label.
Here's suggested code:
String whereMobile = Data.RAW_CONTACT_ID + " = ? AND " +
Data.MIMETYPE + " = ? AND " +
Phone.TYPE + " = ? ";
String[] paramsMobile = new String[]{String.valueOf(rawContactID),
Phone.CONTENT_ITEM_TYPE,
String.valueOf(Phone.TYPE_MOBILE)};
// First delete all the existing phones with type mobile, if any exist
operationList.add(ContentProviderOperation.newDelete(Data.CONTENT_URI)
.withSelection(whereMobile, paramsMobile)
.build());
// Next, insert all the updated phones:
for (UserInfo infoMobileNo : userInfoMobileNos) { // <== change the class to the one you're using in userInfoMobileNos
operationList.add(ContentProviderOperation.newInsert(Data.CONTENT_URI) // <== insert not update!
operationList.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) // <== insert not update!
.withValue(Data.RAW_CONTACT_ID, rawContactID)
.withValue(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE)
.withValue(Phone.TYPE, Phone.TYPE_MOBILE)
.withValue(Phone.NUMBER, infoMobileNo.getPhoneNumber())
.build());
}
String whereGmail = Data.RAW_CONTACT_ID + " = ? AND " +
Data.MIMETYPE + " = ? AND " +
Email.TYPE + " = ? ";
String[] paramsGmail = new String[]{String.valueOf(rawContactID),
Email.CONTENT_ITEM_TYPE,
String.valueOf(Email.TYPE_WORK)};
// First delete all the existing emails with type work, if any exist
operationList.add(ContentProviderOperation.newDelete(Data.CONTENT_URI)
.withSelection(whereGmail, paramsGmail)
.build());
// Next, insert all the updated emails:
for (UserInfo userInfoGmail : userInfoGmails) { // <== change the class here too
operationList.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
.withValue(Data.RAW_CONTACT_ID, rawContactID)
.withValue(Data.MIMETYPE, Email.CONTENT_ITEM_TYPE)
.withValue(Email.TYPE, Email.TYPE_WORK)
.withValue(Email.DATA, userInfoGmail.getEmail())
.build());
}
// don't forget to apply the operations now:
ContentProviderResult[] results = context.getContentResolver().applyBatch(ContactsContract.AUTHORITY, operationList);
Log.d("UPDATE CONTACRT", "results=" + Arrays.toString(results));

Get the list of contact BUT "Other contacts"

I'm using this code to get the list of contacts on the phone.
String[] PROJECTION = new String[] {
ContactsContract.RawContacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts.PHOTO_THUMBNAIL_URI,
ContactsContract.CommonDataKinds.Email.DATA,
ContactsContract.CommonDataKinds.Photo.CONTACT_ID
};
String order = "CASE WHEN " + ContactsContract.Contacts.DISPLAY_NAME + " NOT LIKE '%#%' THEN 1 ELSE 2 END, "
+ ContactsContract.Contacts.DISPLAY_NAME + ", " + ContactsContract.CommonDataKinds.Email.DATA + " COLLATE NOCASE";
String filter = ContactsContract.CommonDataKinds.Email.DATA + " NOT LIKE ''";
Cursor cur = context.getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, PROJECTION, filter, null, order);
if (cur.moveToFirst())
do {
Contact contact = new Contact(
cur.getString(1), // Name
cur.getString(3), // Email
cur.getString(2)); // Photo
if(contacts.indexOf(contact) == -1 && !contact.email.equals(Svc_Profil.getString(ProfilValues.email)))
contacts.add(contact);
} while (cur.moveToNext());
cur.close();
This works very well. The problem is that this list contains all the contacts of the user, even the contacts in "Other Contacts" (which is a list automatically created and completed by Google when you send a mail to an unknown email address, even if you don't add this address to your contacts).
Does somebody know how to not get this "Other Contacts" in the result?
Thank you in advance.

Update a label of email adrress

I need to update the Label email address on phone book of android. Anyone know how i can do this?
Here is my code
String stEtEmail = etEmail.getText().toString(); //EMAIL
String stBtnLabelEmail = etBtnLabelEmail.getText().toString(); //**LABEL**
values.clear();
String mailWhere = ContactsContract.Data.CONTACT_ID + "=? AND " + ContactsContract.Data.MIMETYPE + "=?";
String[] mailWhereParams = new String[]{String.valueOf(idContacto),ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE};
values.put(ContactsContract.CommonDataKinds.Email.DATA,stEtEmail);
cr.update(ContactsContract.Data.CONTENT_URI, values, mailWhere, mailWhereParams);
I don't know how i can update the my label of email.
I have get the solution:
values.put(ContactsContract.CommonDataKinds.Email.TYPE,ContactsContract.CommonDataKinds.Email.TYPE_HOME); //TYPE_WORK TYPE_HOME.......
values.put(ContactsContract.CommonDataKinds.Email.LABEL,stBtnLabelEmail);

Modify native contact programmatically

I'm trying to modify contact first name and last name programmatically.
The code snippet that I've used in order to do the job is the following one:
operations.add( ContentProviderOperation.newUpdate( Data.CONTENT_URI )
.withSelection( RawContacts._ID + "=?",
new String[] { String.valueOf( mSmartphoneContactKey) } )
.withValue( ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME,
mContactName.getEditableText().toString() )
.withValue( ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME,
mContactLastName.getEditableText().toString() )
.build() );
The mSmartphoneContactKey is filled in with the data contained in the column
ContactsContract.Contacts._ID
which is sitting in my projection array when I read contacts using content provider.
The problem is that for some contacts the name and last name are not modified and the phone type is modified instead.
Actually I don't have any clue about the cause.
Any advice is appreciated.
I've read further the documentation the Data table is the one I have to use.
I've modified the code as below...still not working
operations.add( ContentProviderOperation.newUpdate( Data.CONTENT_URI )
.withSelection( Data._ID + " = ? AND " + ContactsContract.Data.MIMETYPE + "='" + ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE + "'",
new String[] { String.valueOf( mSmartphoneContactId ) } )
.withValue( ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, mContactName.getEditableText().toString() )
.withValue( ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, mContactLastName.getEditableText().toString() )
.build() );
Please help me!
Ok Solved!
Wrong ID passed.
Need to retrieve the ID along the data from the DATA table.

Can't add a HOME email has been specified yet to an existing ANDROID contacs

I have a proplem when I make a app "contacts manager" for android.
I have processed creating and deleting a contact with name, and phone
but when I want to add more a home email info to an existing contacts but can't do that.
here my code:
ArrayList ops = new ArrayList();
String selectName = ContactsContract.Data.CONTACT_ID + "= ? AND " + ContactsContract.Data.MIMETYPE + "= ? ";
String[] NameArgs = new String[]{strId, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE};
String selectPhone = ContactsContract.Data.CONTACT_ID + "= ? AND " + ContactsContract.Data.MIMETYPE + "= ? ";
String[] phoneArgs = new String[]{strId, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE};
String selectEmail = ContactsContract.Data.CONTACT_ID + "= ? AND " + ContactsContract.Data.MIMETYPE + "= ? ";
String[] emailArgs = new String[]{strId, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE};
String selectPostAdd = ContactsContract.Data.CONTACT_ID + "= ? AND " + ContactsContract.Data.MIMETYPE + "= ? AND " + ContactsContract.CommonDataKinds.StructuredPostal.TYPE + "= ? ";
String[] postAddArgs = new String[]{strId, ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE, String.valueOf(ContactsContract.CommonDataKinds.StructuredPostal.TYPE_WORK)};
ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
.withSelection(selectName, NameArgs)
.withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, updContact_name.getText().toString())
.build());
ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
.withSelection(selectPhone, phoneArgs)
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, updContact_phone.getText().toString())
.build());
ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
.withSelection(selectEmail, emailArgs)
.withValue(ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Email.DATA, updContact_textMail.getText().toString())
.withValue(ContactsContract.CommonDataKinds.Email.TYPE, ContactsContract.CommonDataKinds.Email.TYPE_WORK)
.build());
ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
.withSelection(selectPostAdd, postAddArgs)
.withValue(ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.StructuredPostal.DATA, updContact_textPostAdd.getText().toString())
.withValue(ContactsContract.CommonDataKinds.StructuredPostal.TYPE, ContactsContract.CommonDataKinds.StructuredPostal.TYPE_HOME)
.build());
getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
this code is available for update a contacts but an Item Info(here is email, StructruedPostal) has been created once. but when I create it with contacts app of android with only name, number (don't touch email, structuedPostal) and save, I don't update it in my android app.
somebody have experience about this pls help me! tks
sorry about my grammar english! It have more mistake and wrong grammar. :(. Tks all
Updating will not insert field which doesn't exist, so follow these steps.
First query for the raw Contact Id of your Contact.
use new Insert method to insert data that has not been inserted to the contact previously when created (in the oppt case newUpdate() will work fine).
A piece of code goes like this
String[] params = new String[] {"Give your Contact"};// you need query and get this too.
String[] proj = {ContactsContract.RawContacts._ID};
Cursor c = context.getContentResolver().query(ContactsContract.RawContacts.CONTENT_URI,proj,
ContactsContract.Data._ID +"= ?", params, null);
long rawContactId = -1;
while(c.moveToNext()){
rawContactId = c.getLong(0);
}
c.close();
After getting those values,
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValue(ContactsContract.Data.RAW_CONTACT_ID, rawContactId)
.withValue(ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Email.DATA, data.getEmail2Address())
.withValue(ContactsContract.CommonDataKinds.Email.TYPE, ContactsContract.CommonDataKinds.Email.TYPE_HOME)
.build());

Categories

Resources