How to delete a contact in android 2.1 - android

I have added one contact to android by following code.
ContentValues values = new ContentValues();
Uri rawContactUri = getContentResolver().insert(RawContacts.CONTENT_URI, values);
long rawContactId = ContentUris.parseId(rawContactUri);
values.clear();
values.put(Data.RAW_CONTACT_ID, rawContactId);
values.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);
values.put(StructuredName.DISPLAY_NAME, "Mike Sullivan");
getContentResolver().insert(Data.CONTENT_URI, values);
It shows up on emulator 2.1, but when i am going to delete it manually by "delete contact" option, its not deleteing from emulator.
If I edit some thing on it then only it deletes.
How can i directly delete it from menu ?
Thanks in advance...

Could you please use the below code to add the contact. This will not affect your emulator to delete contact from menu without editing the same.
import android.provider.Contacts.People;
public void addvaluestocontent()
{
ContentValues values = new ContentValues();
values.put(People.NAME, "Abraham Lincoln");
values.put(People._ID, "1");
values.put(People.NUMBER, "23333");
Uri uri = getContentResolver().insert(People.CONTENT_URI, values);
}

You have to save one more field, either "Given Name" or "Family Name". You can test it manually by saving contacts. First try to save manaully only number and then with saving contacts with both name and number.

Simple, Delete the .db file all contacts were deleted. and android will create new file automatically.
path for .db is:
data/data/com.android.providers.contacts/database/contacts.db

Use this method to Check your SDK version and Get the Contacts content Uri. After, that you can insert the contacts with this new Content Uri,
static
{
int sdk=new Integer(Build.VERSION.SDK).intValue();
if (sdk>=5) {
try {
Class<?> clazz=Class.forName("android.provider.ContactsContract$Contacts");
CONTENT_URI=(Uri)clazz.getField("CONTENT_URI").get(clazz);
}
catch (Throwable t) {
Log.e("PickDemo", "Exception when determining CONTENT_URI", t);
}
}
else {
CONTENT_URI=Contacts.People.CONTENT_URI;
}
}
Refer, CommonsWare Examples for Contacts Content Uri. This may be helps you.

Related

Avoid duplicate entries in SQLite

I am writing a small application in android to store basic details about person using SQLite.
I insert data using this function
public void insertData(String user,String p_no,SQLiteDatabase db)
{
ContentValues cv = new ContentValues();
cv.put(NAME, user);
cv.put(PHONE, p_no);
db.insert("MYTABLE", null, cv);
}
The above function allows duplicate names to be stored.
So I wrote a function that will first check whether a name exits and then enter.
public void insertData(String user,String p_no,SQLiteDatabase db)
{
Cursor resultSet=db.rawQuery("select NAME from MYTABLE where NAME = '"+user+"'",null);
if(resultSet==null)
{
ContentValues cv = new ContentValues();
cv.put(NAME, user);
cv.put(PHONE, p_no);
db.insert("MYTABLE", null, cv);
}
else Toast.makeText(context,user+" already exists",Toast.LENGTH_SHORT).show();
}
But the problem is now toast comes up every time I insert meaning even if the row is unique it is not inserted.
Why resultSet is not null even when there is no such row?
It is because RawQuery never returns null cursor, and that's what your checking criteria is, so it is failing always, and trying to add new value in DB.
I am not able to find the documentation where I learned it, but I will update as soon as possible.
You can check if you have values in cursor using -
if(cursor.moveToFirst())
because it is possible to have an empty cursor. Change your check like
if(cursor.getCount() == 0)
this way, if the cursor is not null, you check if it contains something too.
Probably this is not the best way to handle duplicates, in my opinion. You should mark your column as unique, and use insertWithConflict, to decide what to do in case you have already an entry with that value
Check
if (resultset.getCount() == 0)
Also, create Name as unique key to avoid duplicates. Instead of checking it everytime.

Update display_name of contact 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.

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?)

How to add the new contacts on android 2.2?

I need to add the new contacts on android 2.2 version.
How to add the all fields like Firstname, Lastname, URL, Nickname, IM, Addresses and mobile number?
Take a look at: http://developer.android.com/resources/articles/contacts.html
The process involves some steps, as you would insert the name contact+name first, then field by field.
Example for phone number:
import android.provider.ContactsContract.CommonDataKinds.Phone;
...
ContentValues values = new ContentValues();
values.put(Phone.RAW_CONTACT_ID, rawContactId);
values.put(Phone.NUMBER, phoneNumber);
values.put(Phone.TYPE, Phone.TYPE_MOBILE);
Uri uri = getContentResolver().insert(Phone.CONTENT_URI, values);
Additionally, have a read here: http://developer.android.com/reference/android/provider/ContactsContract.CommonDataKinds.html

how can i get proper Uri of a particular contact in android 2.1

I have written an application and added 2 contacts on emulator, but i
am not able to update their names on android 2.1, code is working on
android 1.6 platform with the following code.
ContentValues contactValues = new ContentValues();
contactValues.put(Contacts.People.NAME, firstName+" "+lastName);
getContentResolver().update(UpdateContactUri, contactValues, null,
null);
In android 1.6 i am getting Uri for those two contacts are "content://
contacts/people/1" and "content://contacts/people/2".
but in 2.1 I am getting these values are "content://contacts/people/8"
and "content://contacts/people/9" and while updating its giving
"java.IllegalArgumentException, Empty values" exception.
When i tried to put a static Uri like "content://contacts/people/1",
code was debugged sucessfully but contact was not updated.
How can i resolve it, why i am not getting uri like 1.6 platform ?
Thanks in advance...
in android 2.1, I use this hack code to update contact name:
public static void modifyPeopleName(ContentResolver cr, String id,
String sName) {
if (sName == null)
return;
ContentValues values = new ContentValues();
int android_sdk_version = Integer.parseInt(Build.VERSION.SDK);
if (android_sdk_version < 7) {
values.put(People.NAME, sName);
cr.update(People.CONTENT_URI, values, People._ID+"="+id, null);
} else {
values.put("data1", sName);
cr.update(Uri.parse("content://com.android.contacts/data/"),
values, "raw_contact_id=" + id, null);
}
return;
}
the 2.1 SDK contains new contentHandler for contacts named ContactsContract
the query now moved to look a it different so i'm sure the URI is different too.
We work in 2.1 only an able to edit and get contact's fields.
see http://developer.android.com/reference/android/provider/ContactsContract.html
You can use below code to add contacts in emulator.
import android.provider.Contacts.People;
public void addvaluestocontent()
{
ContentValues values = new ContentValues();
values.put(People.NAME, "Abraham Lincoln");
values.put(People._ID, "1");
values.put(People.NUMBER, "23333");
Uri uri = getContentResolver().insert(People.CONTENT_URI, values);
}

Categories

Resources