Invalid column in contacts - android

I am planning to create cursorjoiner for my app. I would like to join the Display name and Email id using the cursorjoiner. I am trying on Android 1.6.
When I query for the Email Id list, I get an exception as :
03-30 13:08:15.609: ERROR/AndroidRuntime(302): Caused by: java.lang.IllegalArgumentException: Invalid column person
My code is as below
String[] projection1 = new String[] {
People._ID,
People.DISPLAY_NAME
} ;
String[] projection2 = new String[] {
Contacts.ContactMethods.PERSON_ID,
Contacts.ContactMethods.DATA
} ;
Cursor cur = cr.query(People.CONTENT_URI, projection1, null, null, null);
Cursor emailCur = cr.query( Contacts.ContactMethods.CONTENT_EMAIL_URI,
projection2,
null, null, null);
I checked the docs it says PERSON_ID is a valid entry.
Also, if I use the same column as a parameter inside a query it works.
emailCur = cr.query( Contacts.ContactMethods.CONTENT_EMAIL_URI,
projection2,
Contacts.ContactMethods.PERSON_ID + " = ?",
new String[]{id}, null);
Can any one tell what parameter should I use in projection to achieve this.

Contacts is depreciated, use ContactsContract instead.

Related

Display the Contacts in sorting order ContactsContract.Contacts of Content Resolver

My intention is to display the contacts in sorting order using content resolver in android.
For that I'm writing:
Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID+ " = ?", new String[] { id }, null);
It needs that the last parameter in query method should not be null for sorting the elements by Name. Which part of code I have to replace the null parameter to achieve sorting by name?
To sort result according to name use Phone.DISPLAY_NAME constant with ASC as last parameter to query method. do it as:
Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID+ " = ?",
new String[] { id },
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME+" ASC");
It would be better to use SORT_KEY_PRIMARY or SORT_KEY_ALTERNATIVE on API level 11 and later.
Cursor cursor = getContentResolver().query(
ContactsContract.Contacts.CONTENT_URI,
null, null, null,
ContactsContract.Contacts.SORT_KEY_PRIMARY + " ASC");
You can use Upper() to sort for both lower as well as upper case contact name.
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
null, null, "upper("+ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + ") ASC");
The ContentResolver.query() method takes many arguments but to sort the content provider records, you have to edit the last argument of this method.
It should be like this:
Cursor cursor=getContentProvider().query(.......,"DISPLAY_NAME ASC")
This will arrange the contacts in Ascending order of their name.
Note: This argument should be in a String datatype.
You can use the Desc string using these lines to sort out to see the recent contact
android.database.Cursor cursor = getContentResolver()
.query(android.provider.ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null, null, null,
ContactsContract.Data.CONTACT_LAST_UPDATED_TIMESTAMP +" DESC");

Get RawContactId of specific Contact based off PhoneLookup

I am looking to get the RAW_CONTACT_ID of a specific Contact using PhoneLookup or even just the Contacts LookupKey.
I know the contacts table has a column name_raw_contact_id that references the raw_contacts._id column but it doesn't seem to be returned when querying ContactsContract.Contacts.CONTENT_LOOKUP_URI with the contacts lookup key.
My phone lookup query is:
String[] projection = new String[] {
PhoneLookup.DISPLAY_NAME, PhoneLookup.LOOKUP_KEY };
Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
Cursor c = resolver.query(uri, projection, null, null, null);
Then I am looking up the Contact based on the lookup key:
String[] contactProjection = new String[] {
ContactsContract.Contacts.NAME_RAW_CONTACT_ID
};
Uri contactUri = Uri.withAppendedPath(
ContactsContract.Contacts.CONTENT_LOOKUP_URI, lookupKey);
Cursor contactCursor = resolver.query(contactUri,
contactProjection, null, null, null);
However, this doesn't compile and I get
cannot find symbol: variable NAME_RAW_CONTACT_ID
location: class android.provider.ContactsContract.Contacts
But the android documentation shows NAME_RAW_CONTACT_ID as a column.
Is there any way I can get the RAW_CONTACT_ID based off either phone number or lookup key?
I found that the answer is to make a third query:
long rawContactId = -1;
Cursor c = getContentResolver().query(RawContacts.CONTENT_URI,
new String[]{RawContacts._ID},
RawContacts.CONTACT_ID + "=?",
new String[]{String.valueOf(contactId)}, null);
try {
if (c.moveToFirst()) {
rawContactId = c.getLong(0);
}
} finally {
c.close();
}
But it should be noted that there can be multiple RawContact's per one Contact and the above query will get ALL RawContact's associated with the contactId
ContactsContract.Contacts.NAME_RAW_CONTACT_ID column needs API level 21 or greater so make sure your are compiling with this version.
Also you can use ContactsContract.PhoneLookup._ID column in your first query to get the Contact_Id and then use this Contact_Id in your 3rd query so your problem will be solved in 2 queries instead of 3.
private HashSet<Long> getRawContactIdsForContact(long contactId)
{
HashSet<Long> ids = new HashSet<Long>();
Cursor cursor = context.getContentResolver().query(RawContacts.CONTENT_URI,
new String[]{RawContacts._ID},
RawContacts.CONTACT_ID + "=?",
new String[]{String.valueOf(contactId)}, null);
if (cursor != null && cursor.moveToFirst())
{
do
{
ids.add(cursor.getLong(0));
} while (cursor.moveToNext());
cursor.close();
}
return ids;
}

how to get android contact list data on my seperate listview in android 2.1?

i have created one listview.now i want to show data(contact no.,name) from contactlist of android(Phonebook) on my listview.
i got it for android 1.5 but i want to do it for android2.1updated.
How to do it?can any one guide me or give some sample code?
Thanks in advance---
If you have achieved it in 1.5 then there can be a small difference of URI. You should go for updated/changed URI of Contacts in 2.1. For further understanding here is a sample code for 2.1updated:
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
while (cur.moveToNext())
{
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
//---------------------------------------- Contact Names
//------------------------------------------------------
String DisplayName = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
Cursor names = cr.query(ContactsContract.Data.CONTENT_URI, null,ContactsContract.Data.CONTACT_ID + " = " + id, null, null);
names.moveToNext();
String GivenName = names.getString(names.getColumnIndex(ContactsContract.Data.DATA2));
String FamilyName = names.getString(names.getColumnIndex(ContactsContract.Data.DATA3));
String MiddleName = names.getString(names.getColumnIndex(ContactsContract.Data.DATA5));
names.close();
//----------------------------------------- Phone Numbers
//-------------------------------------------------------
String hasPhoneNumber = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
if (hasPhoneNumber.equals("1"))
{
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ id,null, null);
while (phones.moveToNext())
{
......
}
}
} // AND SO ON.... Get which ever data you need.
so above is just a sample code that I used for my task. You can get what you need by making changes accordingly. I hope it helps.
This will help you getting contacts.
Cursor contactList = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, null,null, null);
For getting phone number of a particular contact,you need to use:
String id = contactList.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
Cursor phoneCursor = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER},
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
new String[]{id}, null);

Android get contacts name and number query

I'm trying to retrieve both the display names and the phone numbers of all my contacts but I want it to return only the rows that have a number.
Currently I have it like this and it works:
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,null, null, ContactsContract.Contacts.DISPLAY_NAME+ " COLLATE NOCASE");
while (cur.moveToNext())
{
if ( Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
{
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
contacts[index] = name;
Cursor pCur = cr.query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", new String[]{id}, null);
pCur.moveToFirst();
numbers[index] = pCur.getString( pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
index++;
pCur.close();
}
The thing is that it takes something like 4-5 seconds to load as it is running the cr.query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", new String[]{id}, null); like 400 times.
Now my understanding is that the name and number of a contact are held in different tables and you need the id of the name in order to get the number.
Can this be done in any other way faster?
Thanks in advance
#mixkat
I have figured out one more solution.
It is possible to get Name and Phone data using just one query.
Here is the code:
String WHERE_CONDITION = ContactsContract.Data.MIMETYPE + " = '" + ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE + "'";
String[] PROJECTION = {ContactsContract.Data.DISPLAY_NAME, ContactsContract.Data.DATA1};
String SORT_ORDER = ContactsContract.Data.DISPLAY_NAME;
Cursor cur = context.getContentResolver().query(
ContactsContract.Data.CONTENT_URI,
PROJECTION,
WHERE_CONDITION,
null,
SORT_ORDER);
In this case you query not Contact provider but Data provider directly.
I have the same problem. I resolved it by saving contacts in db first time user open app. And than I update data (it is up to you when app should update contacts).
So application uses contacts from my db table where name and number are in one row. It takes much less time.

select contact detail where People._ID = contact ID in android

I'm developing application in android. I have contactID, so how can I fetch particular People._ID record from contact list?
If you use the new ContactsContract API:
String selection = ContactsContract.Data.CONTACT_ID +" = ?";
String[] selectionArgs = new String[]{id}
Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, selection, selectionArgs, null);
As you see, you have to use the selection- and selectionArgsParameters of the query-Methode to achieve the where clause.

Categories

Resources