Update display_name of contact Android - android

This method always return false. I would like update display name of contact...
public int updateDisplayName(long id, String newname) {
Uri contact = ContentUris.withAppendedId(Contacts.CONTENT_URI, id);
ContentValues values = new ContentValues();
values.put(Contacts.DISPLAY_NAME, newname);
return context.getContentResolver().update(contact, values, null, null);
}
Can you help me?
Thanks,
Mateus

That's not surprising, this table is for aggregating information of multiple accounts and most of its data is only manipulated by the Contacts content provider itself.
See the documentation: http://developer.android.com/reference/android/provider/ContactsContract.Contacts.html
You would have to change the raw contact.

Related

How to update existing contact using content provider

I am trying to edit the contact of my phone using content provider. To load data i have used below code and it works fine.
private ArrayList<String> getRecords()
{
ArrayList<String> records=new ArrayList<String>();
Cursor cursor=getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,null, null);
if (cursor.moveToFirst())
{
String name="";
String phone="";
String id="";
do{
id=cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone._ID));
name =cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
phone =cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
name = id+" "+name+"\n"+phone;
records.add(name);
}
while(cursor.moveToNext());
}
return records;
}
Now i want to edit actually want to change the name of the selected contact. i am trying below code
Uri uri= ContentUris.withAppendedId(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, id);
ContentValues values=new ContentValues();
values.put(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, "<r XX");
getContentResolver().update(uri, values, null,null);
But it is not updating. What can i do know? Please help. I already check over internet as well as other ans but did not find satisfactory ans.
you dont seem to supply the update paramaters properlly:
the method consist of:
getContentResolver().update(uri, values, where, selectionArgs)
the where should contain:
"ContactsContract.CommonDataKinds.Phone._ID+"=?"
and the selectionArgs should contain the id of the contact to update.

How to update a row in Android database

Hi all I want to update a row on clicking on update button,but its doesn't work.
I have used following code.
public void btnUpdate(View v) {
handeler.updateData(updateName.getText().toString(), updatePhone .getText().toString(), updateEmail.getText().toString(),id);
}
public void updateData(String name, String phone, String email, String id) {
ContentValues values = new ContentValues();
values.put(COLUMN_FIRST, name);
values.put(COLUMN_SECOND, phone); values.put(COLUMN_THIRD, email); database.update(TABLE_NAME, values, id, null);
}
public void search() {
Cursor cursor = handeler.getData();
if (cursor.moveToFirst()) {
String phoneNo;
phoneNo = updateByPhone.getText().toString();
do {
String s1 = cursor.getString(2);
if (phoneNo.compareTo(s1) == 0) {
id = cursor.getString(0);
updateName.setText(cursor.getString(1));
updateEmail.setText(cursor.getString(3));
updatePhone.setText(cursor.getString(2));
}
} while (cursor.moveToNext());
}
}
So if any know please suggest me how to solve it.
Thanks
I see a couple possible issues:
1) You have an extra space updatePhone .getText().toString() should be updatePhone.getText().toString()
2) you are passing a variable id from btnUpdate to updateData but it is not clear where it is coming from (or even if it actually exists)
My bet is that #2 is your issue. You probably need to pass the id (I assume that's meant to be the RowId you want to modify in the db) in to the btnUpdate method:
public void btnUpdate(View v, long id)
There are other possibilities... you haven't shown your DB structure, so it could be that some constraint is causing the update to fail.
EDIT
The update method docs show this:
public int update (String table, ContentValues values, String whereClause, String[] whereArgs)
Note the part String whereClause. That's supposed to be a SQL WHERE statement (without the WHERE). You are only passing in the id, not a WHERE clause. Change your call to update to make that a WHERE clause and it should work. A couple of examples:
database.update(TABLE_NAME, values, "_id = " + id, null);
database.update(TABLE_NAME, values, "_id = '" + id + "'", null);
Both examples assume your row id column is labeled _id.
The first example is if id is an integer value. The issue there is that you are setting id as a string, so I'm unsure if this is the case or not. If it is supposed to be an int, you should get it using id = cursor.getInt(0); instead of id = cursor.getString(0);.
If it truly is a string and not an int, use the second version, which encloses id in single quotes to indicate it is a string.
Hope this helps!
Finally I got solution ,It was minor mistake,Using following code we can perform update operation
database.update(TABLE_NAME, values, BaseColumns._ID + "=" + id, null);
change the code
database.update(TABLE_NAME, values, "_id='?'", new String[]{id});
Edit
database.update(TABLE_NAME, values, "_id=?", new String[]{id});

How to update datas with ContentProvider?

How can I execute this sql with Content Provider as below:
update dtrips set dtp_day_idx=dtp_day_idx+2 where tp_id=1
My java code is like this
DTrip dTrip = new DTrip();
ContentValues values = createContentValues(dTrip);
values.put("dtp_day_idx" ,...);
String select ="tp_id="+tripId;
mContentResolver.update(DTripColumns.CONTENT_URI, values, select, null);
Can anyone help me fix the code?
Thanks.
Append the ID for the row to the content URI like this: (assuming DTripColumns.CONTENT_URI is the content URI of your provider)
final Uri uri = ContentUris.withAppendedId(DTripColumns.CONTENT_URI, tripId);
mContentResolver.update(uri, values, null, null);
Here is more information: http://developer.android.com/guide/topics/providers/content-provider-basics.html

Android: Get CallLog History from a certain contact

I query the CallLog.Calls provider in order to retrieve a list of calls from a certain contact, based on the contact's display name. In particular, I use this query:
String selection = CallLog.Calls.CACHED_NAME + "= ?";
String dispName = dataCollector.getDisplayName();
Cursor callCursor =
cr.query(callLogUri, callLogProjection, selection,
new String[] {dispName},CallLog.Calls.DATE + " DESC");
The dataCollector object is used to hold information from queries based on a given contact id.
The problem is that this code only returns one call for the given contact. I can't understand why. Any clues?
int i=0;
while(cursor.moveToNext())
{
Sring id = cursor.getString(cursor.getColumnIndex(CallLog.Calls._ID));
numbersTemp[i]=cursor.getString(cursor.getColumnIndex(CallLog.Calls.NUMBER));
valuesTemp[i]=cursor.getString(cursor.getColumnIndex(CallLog.Calls.CACHED_NAME));
i++;
}

writing exception to parcel exception while updating contact name in android?

I need to update a column (cached_name) of table callog.calls in sql lite database in android.what I can't figure out is how to use update statement.I am unable to find anything regarding update command using cursor.kindly help.thanks in advance.
//number1 is the phone number against which I need to run update query in db.
//name is the string which I used to insert against number1..
String name=edittext.gettext().tostring();
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(CallLog.Calls.CONTENT_URI,
null, null, null, null);
ContentValues value=new ContentValues();
value.put("CallLog.Calls.CACHED_NAME",name);
cur.moveToFirst();
while(!cur.isLast())
{
String number=cur.getString(cur.getColumnIndex(CallLog.Calls.NUMBER));
if((number.equals(number1)==true)
{
try{
cr.update(CallLog.Calls.CONTENT_URI,value,CallLog.Calls.CACHED_NAME+"=?",null);
}
catch(Exception e)
{
e.printStackTrace();
System.out.println(e.getStackTrace());
}
}//if
cur.moveToNext();
}//while
Basically, the only line you actually need is the update :
String name = edittext.getText().toString();
ContentResolver cr = getContentResolver();
ContentValues value = new ContentValues();
// Note that there is no "" here. The value is a constant
value.put(CallLog.Calls.CACHED_NAME, name);
cr.update(CallLog.Calls.CONTENT_URI, values, CallLog.Calls.NUMBER+"=?",
new String[] { number1 });
This is equivalent to this raw SQL query:
UPDATE call_log SET cached_name = name WHERE number = number1;
There is no need for iterating over all the calls, that's what the where clause is for.
Also,
while(!cur.isLast())
prevents you from actually reading the last matching value. Don't do that. Use
while (cur.moveToNext())
when you need to iterate over a cursor (which you don't, here)
I strongly suggest you take a look at how cursors work, as well as how sql works.
(Also, out of curiosity, why do you need to modify the callLog table?)

Categories

Resources