Find contacts with given display name - android

Learning Android I'm trying to find contacts using DISPLAY_NAME selector. I need to find all contacts with given name. Everything goes great using standard query, but falls when I use ContentProviderOperation. I do not understand some of features. When debugging i see exception: Empty values. But, which values I must insert there? Thanks.
op.add(ContentProviderOperation.newAssertQuery(ContactsContract.Contacts.CONTENT_URI)
.withSelection(ContactsContract.Contacts.DISPLAY_NAME + " = '" + name + "'", new String[] {ContactsContract.Contacts._ID})
.build());
try {
result = getContentResolver().applyBatch(ContactsContract.AUTHORITY, op);
} catch (Exception e) {
}

the problem is with your second line of code which should read:
.withSelection(
ContactsContract.Contacts.DISPLAY_NAME + " = ?",
new String[] {name}
)
To explain, the withSelection method takes two parameters, the selection string and an array selectionArgs, these are substituted into the selection string during query compilation. So in this example the ? is subtituted for the value of name. Text qualifiers (single quotes) are automatically embedded at the same time so no additional effort is required

Related

Unable to delete specific number in a Contact in Android

I'm working on an application in which I should be able to access all the contacts, able to update or delete the numbers in any contact.
I want to delete few numbers in a contact. I'm using batchOperations to perform delete operation.
phone = ContactsContract.Data.RAW_CONTACT_ID + " = ? AND " +
ContactsContract.Data.MIMETYPE + " = ? AND " +
ContactsContract.CommonDataKinds.Phone._ID + " = ?";
String[] phoneArgs = new String[]{Integer.toString(rawContactId), ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE, String.valueOf(id)};
batchOperations.add(ContentProviderOperation.newDelete(Data.CONTENT_URI).withSelection(phone, phoneArgs).build());
this.mContext.getContentResolver().applyBatch("com.android.contacts", batchOperations);
batchOperations.clear();
I'm using this part of code to delete a specific number from the Contact. Using debugger, for a sample delete operation, I found the values as:
raw_contact_id = 4093
id = 21579
These values correspond to specific number("+814444444444") in a group of numbers in a sample contact.(Please refer image below)
But still the number is not getting deleted. I've been trying to figure it out for the last couple of hours but couldn't solve it. Please help.
Instead of supplying selection + selectionArgs, you can build a uri of the specific item you want to delete, like so:
Uri phoneUri = ContentUris.withAppendedId(Data.CONTENT_URI, phoneId);
batchOperations.add(ContentProviderOperation.newDelete(phoneUri).build());
A couple of other notes:
Always check the return value of applyBatch, if it's false, there's some issue with your code / permissions.
Use the constant ContactsContract.AUTHORITY instead of the hard-coded string "com.android.contacts".
Another reason for the observed behavior is that the phone "+814444444444" is stored on multiple RawContacts aggregated into a single contact.
In which case even when you properly delete the row from RawContact A, the contact profile would still get the number from RawContact B.
This is especially true for phone numbers when certain apps such as Whatsapp are installed which tend to copy over contacts' phone numbers to a separate RawContact under their own account.
If that's the issue you'll need to delete the phone number from ALL the contact's RawContacts holding that phone.
EDIT
Here's how to dump all phone numbers that belong to a specific contact, along with the raw-contact that holds them:
String selection = Data.CONTACT_ID + "=? AND " + Data.MIMETYPE + "=?";
String[] selectionArgs = new String[] { contactId.toString(), Phone.CONTENT_ITEM_TYPE };
String[] projection = new String[] { Data.CONTACT_ID, Data.RAW_CONTACT_ID, Data.DATA1 };
Cursor cur = getContentResolver().query(Data.CONTENT_URI, projection, selection, selectionArgs, projection);
DatabaseUtils.dumpCursor(cur);
Also, note that a rawContactId is not an integer, it's a long.

How to delete one row in Android?

I have a problem when to deleting a row in ListView on android, I am using SQLite.
This is my class to delete a file (only need remove item the in database).
public void deleteCallWhenUploadSuccess(String fileNameWhis)
{
db = callDatabaseHelper.getReadableDatabase();
String where = CallDatabaseHelper.FILE_NAME + "=" + fileNameWhis;
db.delete(CallDatabaseHelper.TABLE_NAME, where, null);
}
And in class, I call to using this.
dao.deleteCallWhenUploadSuccess(filename);
But it throws exception:
e: "sqlite.SQLiteException: near "2016": syntax error (code 1):, while compiling:
DELETE FROM recordStatus WHERE fileName=109092 2016-03-17 01.018.03.mp3"
Seem it missing " mark near WHERE "fileName
I tried to add:
String where = CallDatabaseHelper.FILE_NAME + "=" + "'"'" + fileNameWhis;
But the error still exists. How to pass this error? And use DELETE statement to delete a file with fileName, in this case, it has many spaces and special characters in fileName?
Couple of things:
I would do a console log that spits out the file name at the top of the function, so you know its coming in formatted correctly
As pointed out in the other answer you also need to get a writeable database.
Use Where and WhereArgs in your query:
db = callDatabaseHelper.getWriteableDatabase();
String where = CallDatabaseHelper.FILE_NAME + " = ?";
String [] whereArgs = new String[] {fileNameWhis}
db.delete(CallDatabaseHelper.TABLE_NAME, where, whereArgs);
This is a safer way of doing queries, and may solve your issue.
You can delete this in an easier way
db.delete(CallDatabaseHelper.TABLE_NAME, CallDatabaseHelper.FILE_NAME + "=?", new String[]{fileNameWhis});

I can't get data from database using cursor

I have make following function for getting data.
public List<Presentation> getSlideMaster() {
List<Presentation> pptList = new ArrayList<Presentation>();
String selectQuery = "SELECT * FROM "
+ Constants.SLIDE_MASTER.slideMaster_Table + " WHERE dm_Id=" + lastDeckID;
SQLiteDatabase db = dbhelper.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
Presentation presentation = new Presentation();
presentation.setSlideId(cursor.getString(0));
getSlideID = presentation.getSlideId();
Log.i("string slide id in database helper", "" + presentation.getSlideId());
pptList.add(presentation);
} while (cursor.moveToNext());
}
String selectQuery2 = "SELECT sl_Id FROM "
+ Constants.SLIDE_LAYOUT.slideLayout_Table + " WHERE sm_Id = "
+ getSlideID;
Cursor cursorSL = db.rawQuery(selectQuery2, null);
Log.i("query", ""+selectQuery2);
if(cursorSL.moveToFirst()){
Log.i("getslideLayout function", "");
do{
Log.i("query string 0 ", ""+cursorSL.getString(0));
Presentation presentation = new Presentation();
presentation.setSlideLayoutId(cursorSL.getString(0));
getSlideLayoutId = presentation.getSlideLayoutId();
Log.i("string slide layout id in slideMaster", "" + getSlideLayoutId);
}while(cursorSL.moveToNext());
}
else{
Log.i("No Data Found!!!!!", "");
}
cursor.close();
cursorSL.close();
db.close();
return pptList;
}
I getting data from slideMaster_Table very fine and easily but I can't get data from another query, which is not print any value in Logcat inside of if(cursorSL.moveToFirst()) condition
When I use code in another activity i can get sl_Id very easily.
may below solution solve your problem
use Cursor cursorSL = db.rawQuery(selectQuery2, new String[]{getSlideID});
for
selectQuery2="SELECT sl_Id FROM "+Constants.SLIDE_LAYOUT.slideLayout_Table + " WHERE sm_Id = ?";
My speculation is that you have troubles constructing the second query. Basically it is never a good idea to construct database queries like you do. The android database API is providing more accurate interface for that. Rewrite your second part of program to:
String [] selectArgs = {getSlideID};
String selectQuery2 = "sm_Id = ?";
Cursor cursorSL = db.query(Constants.SLIDE_LAYOUT.slideLayout_Table, null,
selectQuery2, selectArgs, null, null, null, null);
Note that the fourth parameter of query is meant for substitution of where arguments, it is String[] and all the array members are substituted in sequence in the places of ? in the query string.
I think you might be facing problem with constructing the query.
PS: You are also misusing the log tags:
Log.i("No Data Found!!!!!", "");
Should actually be Log.i("TAG", "No Data Found!!!!!");
The first parameter is log tag and is supposed to be used for grouping related log messages, in your case you print every message in its own group, which does not make much sense.
I know this note will not solve your immediate problem, but I am pointing it out, so that you will know it for the future (I also suffer from seeing colleagues of mine learning from SO wrong practises like that one).

Cursor adapter _id move forward by 1

I'm having some issues working with a CursorAdapter.
In bindView(), I retrieve data in this way:
final String id = c.getString(c.getColumnIndexOrThrow(MySQLiteHelper.PROF_CONTACTS_KEY_ID));
final String name = c.getString(c.getColumnIndexOrThrow(MySQLiteHelper.PROF_CONTACTS_KEY_NAME));
Right after this code, I call
Log.e("Log",id+" <=> "+name);
But, because of some weird problem, I got as a result an ID moved forward by 1.
This is the situation in the DB (pulling it from the emulator, and opening it with SQLite Manager):
And this is the output:
With bigger numbers (>9), IDs start to mess even more up: number 10 becomes number 1, number 13 becomes number 5, etc.
I wouldn't have a lot of problems, in fact the only thing not matching is the id, all other info correspond, but I have a details activity to which I pass the ID in order to show to the user the detailed info.
This is the piece of code where I apply the adapter:
mCursor = mDb.rawGet("SELECT * FROM "+MySQLiteHelper.PROF_CONTACTS_TB_NAME+" LEFT JOIN "+
MySQLiteHelper.EXAMS_TB_NAME+" ON "+
MySQLiteHelper.PROF_CONTACTS_TB_NAME+"."+MySQLiteHelper.PROF_CONTACTS_KEY_COD_ESAME+"="+
MySQLiteHelper.EXAMS_TB_NAME+"."+MySQLiteHelper.EXAMS_KEY_COD
+ " ORDER BY " + MySQLiteHelper.PROF_CONTACTS_TB_NAME+"."+MySQLiteHelper.PROF_CONTACTS_KEY_ID);
if (mCursor.getCount() == 0) {
// error stuff.
} else {
String[] columns = new String[] {};
int[] to = new int[] {};
mDataAdapter = new CursorAdapterProfContacts(getSherlockActivity(), R.layout.item_prof_contact, mCursor, columns, to, 0);
mLvContacts.setAdapter(mDataAdapter);
}
Move the cursor to the first row,after initial cursor like,
mCursor.moveToFirst()
Are you sure that you have _id correctly populated when you insert a value? You can extract the database if you use the emulator and open it with SQLiteManager plugin for Firefox. As well, instead of quering all with *, use the same projection column names as you use inside y our bindView(); something is not matching here
It was due to a collision name: _id can be referred both to EXAMS and PROF. SQLlite chose EXAMS instead of PROF.
mCursor = mDb.rawGet("SELECT *, "+
MySQLiteHelper.PROF_CONTACTS_TB_NAME+"."+MySQLiteHelper.PROF_CONTACTS_KEY_ID+" AS idProf "+
" FROM "+MySQLiteHelper.PROF_CONTACTS_TB_NAME+" LEFT JOIN "+
MySQLiteHelper.EXAMS_TB_NAME+" ON "+
MySQLiteHelper.PROF_CONTACTS_TB_NAME+"."+MySQLiteHelper.PROF_CONTACTS_KEY_COD_ESAME+"="+
MySQLiteHelper.EXAMS_TB_NAME+"."+MySQLiteHelper.EXAMS_KEY_COD +
" ORDER BY " + MySQLiteHelper.PROF_CONTACTS_TB_NAME+"."+MySQLiteHelper.PROF_CONTACTS_KEY_ID);
And finally
final Long id = c.getLong(c.getColumnIndexOrThrow("idProf"));
This made the trick.
Collision name errors should be thrown, as it is in SQL and MySQL.

Android change number to primary number

I'm trying to make someones number a primary number in the specific contact numbers. This is the code:
Cursor the_phone = _context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null, ContactsContract.CommonDataKinds.Phone.NUMBER +" = "+ numberToCall, null, null);
ContentValues values = new ContentValues();
if (the_phone.moveToFirst()){
values.put(ContactsContract.CommonDataKinds.Phone.LABEL,
the_phone.getString(the_phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.LABEL)));
values.put(ContactsContract.CommonDataKinds.Phone.IS_PRIMARY,1);
values.put(ContactsContract.CommonDataKinds.Phone.NUMBER,numberToCall);
int phones = _context.getContentResolver().update(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,values, ContactsContract.CommonDataKinds.Phone.NUMBER +" = "+ numberToCall, null);
}
Then I keep getting the following:
03-27 08:18:27.009: E/AndroidRuntime(640): FATAL EXCEPTION: main
03-27 08:18:27.009: E/AndroidRuntime(640): java.lang.UnsupportedOperationException: URI: content://com.android.contacts/data/phones, calling user: com...
I checked the first query and it's working I'm able to find the number in the resolver but unable to update it.
ok actually solved after looking into the resolver class.
private Boolean editPrimary( Cursor phones , String contactId, String contactNumber, int primaryTo){
ArrayList ops = new ArrayList();
String where = ContactsContract.Data.CONTACT_ID + " = ? AND " +
ContactsContract.CommonDataKinds.Phone.MIMETYPE + " = ? AND " +
String.valueOf(ContactsContract.CommonDataKinds.Phone.TYPE) + " = ? AND " +
ContactsContract.CommonDataKinds.Phone.NUMBER + " = ?";
String[] params = new String[] {contactId,
ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE,
String.valueOf(phones.getInt(phones.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.TYPE))),
contactNumber};
ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
.withSelection(where, params)
.withValue(ContactsContract.CommonDataKinds.Phone.IS_SUPER_PRIMARY,primaryTo)
.build());
try {
_context.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
return true;
} catch (RemoteException e) {
e.printStackTrace();
return false;
} catch (OperationApplicationException e) {
e.printStackTrace();
return false;
}
}
from the developer guide we can see that :
Overview
ContactsContract defines an extensible database of contact-related information. Contact information is stored in a three-tier data model:
A row in the ContactsContract.Data table can store any kind of personal data, such as a phone number or email addresses. The set of data kinds that can be stored in this table is open-ended. There is a predefined set of common kinds, but any application can add its own data kinds.
A row in the ContactsContract.RawContacts table represents a set of data describing a person and associated with a single account (for example, one of the user's Gmail accounts).
A row in the ContactsContract.Contacts table represents an aggregate of one or more RawContacts presumably describing the same person. When data in or associated with the RawContacts table is changed, the affected aggregate contacts are updated as necessary.
so my first approach was wrong i didn't realize that the datakind class could represent the same data as the data class only directed to the context we are looking at

Categories

Resources