I cant update the notes section of a contact - android

I'm trying to change the notes section of a contact, I got their phone number (receivedLocationSender) and the Log output gives the correct Name and ID, but idk how to get it to replace the "NOTES" section of the contact.. what I currently have does absolutely nothing.
private void displayContacts() {
ContentResolver contentResolver = getBaseContext().getContentResolver();
ContentValues contentValues = new ContentValues();
Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(receivedLocationSender));
String[] projection = new String[] {PhoneLookup.DISPLAY_NAME, PhoneLookup._ID};
Cursor cursor = contentResolver.query(
uri,
projection,
null,
null,
null);
if(cursor!=null) {
while(cursor.moveToNext()){
String contactName = cursor.getString(cursor.getColumnIndexOrThrow(PhoneLookup.DISPLAY_NAME));
String contactId = cursor.getString(cursor.getColumnIndexOrThrow(PhoneLookup._ID));
contentValues.clear();
String noteWhereParams = ContactsContract.CommonDataKinds.Note.NOTE;
String[] args = new String[] { String.valueOf(receivedLocation) };
contentValues.put(ContactsContract.CommonDataKinds.Note.NOTE, receivedLocation);
getContentResolver().update(ContactsContract.Data.CONTENT_URI, contentValues, contactId + "=?", args);
Log.d(LOGTAG, "contactMatch name: " + contactName);
Log.d(LOGTAG, "contactMatch id: " + contactId);
Log.d(LOGTAG, "contactNotes : " + ContactsContract.CommonDataKinds.Note.NOTE.toString());
}
cursor.close();
}
}

After you have a contact id (id) from phonelookup.. the stuff below works
ContentResolver cr = this.getContentResolver();
ContentValues values = new ContentValues();
values.clear();
String noteWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
String[] noteWhereParams = new String[]{id,ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE};
values.put(CommonDataKinds.Note.NOTE, "NEW NOTE HERE!!!!");
cr.update(ContactsContract.Data.CONTENT_URI, values, noteWhere, noteWhereParams);
Cursor noteCur = cr.query(ContactsContract.Data.CONTENT_URI, null, noteWhere, noteWhereParams, null);
if (noteCur.moveToFirst()) {
String note = noteCur.getString(noteCur.getColumnIndex(ContactsContract.CommonDataKinds.Note.NOTE));
Log.d(LOGTAG, "notes : " + note);
}
noteCur.close();

Related

Retrieving contacts

I have tried to get phone number also but i didn't no how to get using this code . This the code i am using please tell how to get number by same code for same id
// method to get name, contact id, and birthday
private Cursor getContactsBirthdays() {
Uri uri = ContactsContract.Data.CONTENT_URI;
String[] projection = new String[] {
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Event.CONTACT_ID,
ContactsContract.CommonDataKinds.Event.START_DATE
};
String where =
ContactsContract.Data.MIMETYPE + "= ? AND " +
ContactsContract.CommonDataKinds.Event.TYPE + "=" +
ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY;
String[] selectionArgs = new String[] {
ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE
};
String sortOrder = null;
return managedQuery(uri, projection, where, selectionArgs, sortOrder);
}
// iterate through all Contact's Birthdays and print in log
Cursor cursor = getContactsBirthdays();
int bDayColumn = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Event.START_DATE);
while (cursor.moveToNext()) {
String bDay = cursor.getString(bDayColumn);
Log.d(TAG, "Birthday: " + bDay);
}
You need to first get a list of all contact-ids that has birthdays, then query for the phones of all those contacts, and then print the combined results.
Cursor cursor = getContactsBirthdays();
// get contact-ids for phones query
List<String> ids = new ArrayList<>();
while (cursor.moveToNext()) {
ids.add(cursor.getString(1));
}
String[] projection = new String[] { Phone.NUMBER, Phone.CONTACT_ID };
StringBuilder where = new StringBuilder(Data.MIMETYPE + " = " Phone.CONTENT_ITEM_TYPE + " AND " + Contacts.CONTACT_ID + " IN (");
for (String id : ids) {
where.append(id).append(",");
}
where.deleteCharAt(where.length() - 1);
where.append(")");
Cursor cur2 = getContentResolver().query(Data.CONTENT_URI, projection, where.toString(), null, null);
Map<Long, String> contactIdToPhone = new HashMap<>();
while (cur2.moveToNext()) {
contactIdToPhone.put(cur2.get(1), cur2.get(0));
}
cur2.close();
cursor.moveToPosition(-1);
while (cursor.moveToNext()) {
Long id = cursor.getLong(1);
Log.i(TAG, "Birthday: id=" + id + ", name=" + cursor.getString(0) + ", date=" + cursor.getString(2) + ", phone=" + contactIdToPhone.get(id));
}
cursor.close();

Cursor query order by Date

I have a TextView to which I'm putting my contacts which have birthdays:
private String loadContacts()
{
StringBuilder builder = new StringBuilder();
ContentResolver cr = getContentResolver();
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null,
null, null);
if (cursor.getCount() > 0){
while (cursor.moveToNext()){
String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String columns[] = {
ContactsContract.CommonDataKinds.Event.START_DATE,
ContactsContract.CommonDataKinds.Event.TYPE,
ContactsContract.CommonDataKinds.Event.MIMETYPE,
};
String where = ContactsContract.CommonDataKinds.Event.TYPE + "=" +
ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY +
" and " + ContactsContract.CommonDataKinds.Event.MIMETYPE +
" = '" + ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE +
"' and " + ContactsContract.Data.CONTACT_ID + " = " + id;
String sortOrder = ContactsContract.CommonDataKinds.Event.START_DATE + " ASC";
Cursor birthdayCur = cr.query(ContactsContract.Data.CONTENT_URI, columns, where,
null, sortOrder);
if (birthdayCur.getCount() > 0) {
while (birthdayCur.moveToNext()) {
String birthday = birthdayCur.getString(birthdayCur.
getColumnIndex(ContactsContract.CommonDataKinds.Event.START_DATE));
builder.append("Contact: ").append(name).append(" ").append(birthday).append("\n\n");
}
}
birthdayCur.close();
}
}
cursor.close();
return builder.toString();
}
And then assign the contacts on the "onCreate" function
if(ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED)
{
ActivityCompat.requestPermissions(this, new String[] { Manifest.permission.READ_CONTACTS }, 1);
} else {
listContacts.setText(loadContacts());
}
This line of code:
String sortOrder = ContactsContract.CommonDataKinds.Event.START_DATE + " ASC";
Cursor birthdayCur = cr.query(ContactsContract.Data.CONTENT_URI, columns, where,
null, sortOrder);
doesn't seem to work no matter what I put there to order it (name, _ID etc.).
I am trying to sort by Birthdays but even if I try to sort by name it doesn't work. Why ?
Am I using wrong type of View(TextView) or is it the way I use StringBuilder to set the text or something else entirely?
My minSdkVersion is 19 and targetSdkVersion 26. I'm testing this App on my Nexus5(api 23 android 6.01) on debugging mode.
I appreciate any advice.
Well this is what happens when I hurried. It was actually beyond simple.
I just needed to put all my needed columns in the first Cursor and the second more important thing was to use "ContactsContract.Data.CONTENT_URI"
instead of "ContactsContract.Contacts.CONTENT_URI" because the "DATA.CONTENT_URI" actually has all of my needed columns and "Contacts.CONTENT_URI" doesn't.
As simple as that.
private String loadContacts()
{
StringBuilder builder = new StringBuilder();
ContentResolver cr = getContentResolver();
String columns[] = {
ContactsContract.CommonDataKinds.Event.START_DATE,
ContactsContract.Contacts.DISPLAY_NAME
};
String where = ContactsContract.CommonDataKinds.Event.TYPE + "=" +
ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY +
" and " + ContactsContract.CommonDataKinds.Event.MIMETYPE +
" = '" + ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE + "'";
String sortOrder = ContactsContract.CommonDataKinds.Event.START_DATE + " ASC";
Cursor cursor = cr.query(ContactsContract.Data.CONTENT_URI,
columns, where,
null, sortOrder);
if (cursor.getCount() > 0){
while (cursor.moveToNext()){
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String birthday = cursor.getString(cursor.
getColumnIndex(ContactsContract.CommonDataKinds.Event.START_DATE));
builder.append("Contact: ").append(name).append(birthday).append(" ").append("\n\n");
}
}
cursor.close();
return builder.toString();
}

Android - SearchView - cursor - Reyclerview empty

I'm trying to get contacts with their email addresses and their phone numbers.
For some reason, I only get 1 email, even though the cursor getcount returns 7790
Thank you very much for any help
public void getNameEmailDetails(){
ContentResolver contactResolver = context.getContentResolver();
Cursor cursor = contactResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
if (cursor != null && cursor.moveToFirst()) {
Log.d("TAG", String.valueOf(cursor.getCount()));
String displayName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String photoUri = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.PHOTO_THUMBNAIL_URI));
String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
String lookupKey = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
if (Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
{
Cursor pCur = contactResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { contactId }, null);
while (pCur.moveToNext())
{
String phone = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
String type = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
String s = (String) ContactsContract.CommonDataKinds.Phone.getTypeLabel(context.getResources(), Integer.parseInt(type), "");
Log.d("TAG", s + " phone: " + phone);
}
pCur.close();
}
Cursor emailCursor = contactResolver.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", new String[] { contactId }, null);
while (emailCursor.moveToNext())
{
String phone = emailCursor.getString(emailCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
int type = emailCursor.getInt(emailCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
String s = (String) ContactsContract.CommonDataKinds.Email.getTypeLabel(context.getResources(), type, "");
Log.d("TAG", s + " email: " + phone);
}
emailCursor.close();
cursor.close();
}
}

Not able to fetch the correct Birthday and Anniversary data from Contact

I am able to fetch other information (Display name,organisation,phone no and email_id) of a contact, but not able to fetch birthday and anniversary of that contact.
Here is the code i am using for birthday. It does fetch the data, but gives me wrong data, i.e repeats the same data for all the contacts.
private String getBDate(String id) {
String bday = null;
ContentResolver cr = getContentResolver();
Uri uri = ContactsContract.Data.CONTENT_URI;
String[] projection = new String[] {
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Event.CONTACT_ID,
ContactsContract.CommonDataKinds.Event.START_DATE };
String where = ContactsContract.Data.MIMETYPE + "= ? AND "
+ ContactsContract.CommonDataKinds.Event.TYPE + "="
+ ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY;
String[] selectionArgs = new String[] { ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE };
String sortOrder = null;
Cursor cur = cr.query(uri, projection, where, selectionArgs, sortOrder);
while (cur.moveToNext()) {
bday = cur
.getString(cur
.getColumnIndex(ContactsContract.CommonDataKinds.Event.START_DATE));
Log.v("Birthday", bday);
}
cur.close();
return bday;
}
Same is the case with anniversary, here is the code for it. In some case anniversary is not added but it still shows the data from other contact.
private String getAnnv(String id) {
String annv = null;
ContentResolver cr = getContentResolver();
Uri uri = ContactsContract.Data.CONTENT_URI;
String[] projection = new String[] {
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Event.CONTACT_ID,
ContactsContract.CommonDataKinds.Event.START_DATE };
String where = ContactsContract.Data.MIMETYPE + "= ? AND "
+ ContactsContract.CommonDataKinds.Event.TYPE + "="
+ ContactsContract.CommonDataKinds.Event.TYPE_ANNIVERSARY;
String[] selectionArgs = new String[] { ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE };
// String sortOrder = null;
Cursor cur = cr.query(uri, projection, where, selectionArgs, null);
while (cur.moveToNext()) {
annv = cur
.getString(cur
.getColumnIndex(ContactsContract.CommonDataKinds.Event.START_DATE));
Log.v("Anniversary", annv);
}
cur.close();
return annv;
}
you are not using String id perameter in where condition so please check again.
E,g private String getAnnv(String id) function has input for ID but that seems to be not used withing function so please put that ID in condition check and this should work.
e.g
ContactsContract.CommonDataKinds.Event.CONTACT_ID + "= " + ID
AND ContactsContract.Data.MIMETYPE + "= ? AND "

Retrieving group of particular contact

I want to retrieve the contact details along with the group which it belongs to. I got the code to list all the contact groups in the phone.
Cursor groupC = getContentResolver().query(
ContactsContract.Groups.CONTENT_URI, null, null, null, null);
while (groupC.moveToNext()) {
String groupid =
groupC.getString(groupC.getColumnIndex(ContactsContract.Groups._ID));
Log.e("myTag", groupid);
String grouptitle =
groupC .getString(groupC.getColumnIndex(ContactsContract.Groups.TITLE));
Log.e("myTag", grouptitle);
}
groupC.close();
Then I tried to query for a particular contact by using its id but it always shows There is no such column....
Cursor groupC = getContentResolver().query(
ContactsContract.Groups.CONTENT_URI,
null,
ContactsContract.Contacts._ID+"= ?",
new String[]{id},
null);
where id is
Cursor cur = cr.query(
ContactsContract.Contacts.CONTENT_URI,
null,
null,
null,
null);
id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
How to query the group using a particular contact id?
I found the answer.we should pass the raw contact-id and the correct mime type.
String where = ContactsContract.Data.RAW_CONTACT_ID
+ "="
+ Integer.parseInt(id)
+ " AND "
+ ContactsContract.Data.MIMETYPE
+ "='"
+ ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE
+ "'";
Cursor cursor = ctx
.getContentResolver()
.query(ContactsContract.Data.CONTENT_URI, null, where, null,
null);
startManagingCursor(cursor);
Log.e("Count is:", ""+ cursor.getCount());
while (cursor.moveToNext()) {
groupid = cursor
.getString(cursor.getColumnIndex(ContactsContract.Data.DATA1));
Log.e("groupid", groupid);
builder.append(groupid);
}String where = ContactsContract.Data.RAW_CONTACT_ID
+ "="
+ Integer.parseInt(id)
+ " AND "
+ ContactsContract.Data.MIMETYPE
+ "='"
+ ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE
+ "'";
Cursor cursor = ctx
.getContentResolver()
.query(ContactsContract.Data.CONTENT_URI, null, where, null,
null);
startManagingCursor(cursor);
Log.e("Count is:", ""+ cursor.getCount());
while (cursor.moveToNext()) {
groupid = cursor
.getString(cursor.getColumnIndex(ContactsContract.Data.DATA1));
Log.e("groupid", groupid);
break;
}
A contact may be in more than one group,here it retrivr its first group pnly.
I think this may be useful to somebody...

Categories

Resources