Android Contacts Group changes - android

I have two contacts group on my device like Work, Home. I have to add contact on that particular group. Like Add john(Contacts name0) to Work group.
I have used following Code:
ContentValues values = new ContentValues();
values.put(ContactsContract.CommonDataKinds.GroupMembership.RAW_CONTACT_ID,
Integer.parseInt(contactId));
values.put(
ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID,
"3");
values
.put(
ContactsContract.CommonDataKinds.GroupMembership.MIMETYPE,
ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE);
context.getContentResolver().insert(
ContactsContract.Data.CONTENT_URI, values);
But I get the following output: content://com.android.contacts/data/714
Please guide me, what am I doing wrong?
Thanks in advance!

I don't see anything wrong! What makes you think something is wrong?
Looks like you're trying to add the group identified by "3" for the raw contact identified by contactId. The result is contactId now belongs to group "3" (You have to look up "3" in ContactsContract.Groups).
What this does is add a GroupMembership row to the Data table. Group membership is an attribute of the raw contact contactId. So you've done what you set out to do. The result is an Uri that points to the data row you added.
I figure you were expecting something else. Can you elaborate?

Related

Update contact name in android 4.4

I would like to modify contact's name in an Android device (GT N5110) with Android 4.4. I have tried to do that by this approach:
ContentValues contentValues = new ContentValues();
String selection = ContactsContract.Contacts._ID + " = ? " ;
String[] selectionArgs = new String[] { Integer.toString(id) };
contentValues.put(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, lastName);
contentValues.put(ContactsContract.Contacts.STARRED, 2); // *
contentValues.put(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, lastName);
contentValues.put(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, firstName);
return this.context.getContentResolver().update(ContactsContract.Contacts.CONTENT_URI,
contentValues, selection, selectionArgs);
The line marked with * was the only one which modified the contact database. I also tried by applyBatch and added the MIMETYPE selection (ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE). The MIMETYPE approach throws some Exception with these message:
android.database.sqlite.SQLiteException: no such column: mimetype (code 1): , while compiling: SELECT _id FROM view_contacts_restricted WHERE _id = ? AND mimetype=?
I researched and tried suggestions present in update contacts display_name and Modifying contact information.
Can someone help me?
The following website explains it to you. It is a full tutorial by android itself. I highly recommend you use this website to first check if there is an answer to your query first before coming here.
http://developer.android.com/reference/android/provider/ContactsContract.Intents.html#EXTRA_FORCE_CREATE
What this website explains to you is
Send data to the contact you require to edit and it automatically opens the contact edit screen with the details which you passed to it.
If you want to know what all extras you can send to it. Visit the following link.
https://developer.android.com/reference/android/provider/ContactsContract.Intents.Insert.html
I am not sure if this is correct but it may be because of some security policy or so that you can't skip ui confirmation. But again I am not familiar with this so I may be wrong. I have not tested if this works or anything but I think it might help. Especially the second one:
http://developer.android.com/reference/android/provider/ContactsContract.Intents.html#EXTRA_FORCE_CREATE
And
http://www.techjini.com/blog/2011/10/31/insert-and-modify-contact-in-android/

Android Sqlite : assign one column's values to another using ContentValues

I want to assign one column's values to another.
here is a query for it.
UPDATE contacts SET contactName=phone;
Here contactName and phone are two different columns.i am assigning phone column's values to contactName column.I can do this by using
db.execSQL(statement);
but if i want it to update it using
db.update(tableName, cValues, whereClause, whereArgs);
what should i put in Content values.If i put phone as a value then it will consider like
UPDATE contacts SET contactName="phone";
Is it possible using content values?
Help me to solve this issue.
Any help will be appreciated.Thanks in advance
You can't use ContentValues for that. You can only update/insert literal values with ContentValues, not column name references.
Just use execSQL() with the raw SQL you have. (Don't use rawQuery() - it alone won't execute your SQL.)

Android SQLite Update stops working after calling it two times

I have problems in updating rows in SQLite database in my Android application. It works successfully only, if I update it two times. But when I try to do it on the third time, it doesn't update the same row anymore.
LogCat doesn't show any exceptions. db.update() returns '1'.
I've searched similar issues on StackOverflow and the web. People advic]sed to remove db.close(); from database-helper, because I call it several times, or to use db.update method instead of db.rawQuery() or db.execSQL().
I also tested my query in SQLite client, and it works as it's supposed to.
Here is code of simple database-helper method:
public int updateEventDoneMark(Event event)
{
ContentValues args = new ContentValues();
args.put("completed", event.getCompleted());
return db.update("Event", args, "id" + "='" +event.getId() + "'", null);
}
Is there some SQLite-related issue I should know while I update one database entry several times in a row?
What does your content provider update and URI match look like?
Typical Content providers have a URI for each Table/View for a single row where _id is passed as a where_argument and a URI for multiple rows which uses where and where_arguments to select the rows to be updated.
Also it looks like you update by id. Android really want the id column named "_id", although I don't think is currently your issue, but it really depends on the URI it's using. Content Providers are usually coded with the _id and select by the column for a single row based on _id. That's why I want to see content provider. Your also selecting by the id yourself, this doesn't seem normal, although it could be accomplished, but not the norm. Typically the where part is something like 'colunm name = ?" and the next parameter where_arguments is a string array containing the value to replace the '?'.
Hope this helps.

ContactsContract.Data._ID value is always the same

I am querying a user profile data on an Android Phone using the following URI:
Uri.withAppendedPath(Profile.CONTENT_URI, ContactsContract.Contacts.Entity.CONTENT_DIRECTORY);
All ContactsContract.Data rows seem to be returned correctly (structured names, phones,email, web, etc). The problem I am having is that
curData.getLong(curData.getColumnIndex(ContactsContract.Data._ID));
returns the same value for every row, even though other columns have different values. Could somebody help me understand why is this happening? How can I distinguish one ContactsContract.Data row from another?
Just found out that Entity.DATA_ID should be used instead of ContactsContract.Data._ID when querying ContactsContract.Data rows through Entity URIs.

Deleting a specific RawContact content directory entry

I'm new to android dev, so I may get the whole concept totally wrong. I want to delete a specific entry from RawContact directory entry. Here is code that I have:
Uri rawContactUri = ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId);
Uri entityUri = Uri.withAppendedPath(rawContactUri, Entity.CONTENT_DIRECTORY);
Cursor c = getContentResolver().query(entityUri,
new String[]{RawContacts._ID, Entity.DATA_ID, Entity.MIMETYPE,CommonDataKinds.GroupMembership.GROUP_SOURCE_ID},
null, null, null);
using cursor c I get appropriate Entity.DATA_ID. After that I try to delete an entry:
getContentResolver().delete(entityUri,Entity.DATA_ID+"=?",
new String[]{id});
and get an error:
java.lang.UnsupportedOperationException: URI:
content://com.android.contacts/raw_contacts/2709/entity
What am I doing wrong?
UPD 1
I am trying to remove group membership entry.
Please give a more complete explanation of what you're trying to do. You say that you want to "delete a specific entry from RawContact directory entry.", which is confusing? Do you want to
a) delete a raw contact?
b) delete a set of raw contacts?
c) delete all of the data rows for a single raw contact?
d) delete all of the data rows for a set of raw contacts?
or do you want to do something with group membership?
In any event, I think you've constructed the URI backwards. Try appending Entity.CONTENT_DIRECTORY before the rawContactId. I know that the documentation doesn't say this, but the documentation is not well-written.
A better alternative would be to use the ContactsContract.RawContactEntity table.
Looks like I was using the wrong URI. Also I switched to a "new" way of modifying the table:
ArrayList<ContentProviderOperation> ops =
new ArrayList<ContentProviderOperation>();
ops.add(ContentProviderOperation.newDelete(Data.CONTENT_URI)
.withSelection(Data._ID + "=?", new String[]{i})
.build());
getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);

Categories

Resources