Android: Show all contacts sorted by favorites - android

I know how to fetch all contacts as well as how to fetch the favorited contacts.
Is there a way to combine the two and sort by favorites?
getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, "starred=?", new String[] {"1"}, <sort by favorites?>);

Try to use this query to get all contacts order by favorites and then by display name.
getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
null,
null,
ContactsContract.Contacts.STARRED + " DESC, " + ContactsContract.Contacts.DISPLAY_NAME_PRIMARY + " ASC");

Related

Get the Max _id from ContentResolver from Contacts.CONTENT_URI

I want to get the last added contact in the contacts and for getting that i want to pull the max _id of the contact. So here is my query that i want to realise:
AppMain.applicationContext.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,
new String[]{
"MAX(" + ContactsContract.Contacts._ID + ") as max_id",
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.HAS_PHONE_NUMBER
},
null, null, null);
But unfortunately i am getting this error:
Invalid column MAX(contact_id) as max_id
I tried removing the 'as max_id' but no luck.
Does any one know how to get the last added contact or get the max _id of the contact.
This is not officially supported by Android's ContentProvider framework, especially if the Provider set the strictProjectionMap flag.
But this should work instead, it asks for all contacts sorted by contact-id, and limits the results to 1:
Cursor c = cr.query(Contacts.CONTENT_URI, new String[] { Contacts._ID }, null, null, Contacts._ID + "DESC LIMIT 1");
if (c != null && c.moveToNext()) {
Log.d(TAG, "id is: " + c.getLong(0));
}

Contact list not sort in Android

I am trying to fetch my phone contacts in alphabetical sort order.Its fetch name by fast but not getting sort order .I tried ContactsContract.Contacts.SORT_KEY_PRIMARY + " ASC" also ContactsContract.Contacts.DISPLAY_NAME + " ASC" but not getting good result.
My code is
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null,ContactsContract.Contacts.DISPLAY_NAME + " ASC");
while (phones.moveToNext())
{
String name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Log.e("Tag ","Name " + name);
}
phones.close();
you have to get the sort order of your phone contacts like this:
int sort_order=Settings.system.getInt (getApplicationContext ().getContentResolver (),"android.contacts.SORT_ORDER");
now your cursor query will be like this:
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null,sort_order);
you can get contacts in alphabatical order:
Cursor cursor = getContentResolver.query(Phone.CONTENT_URI, null, null, null, Phone.DISPLAY_NAME + " ASC");

Android Contacts._ID != Data.CONTACT_ID

I'm running a query against CommonDataKinds.Phone.CONTENT_URI and I'm getting all the results that have a NOT NULL phone id.
pretty much the code is :
String[] projection2 = new String[] {
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
};
String where2 = ContactsContract.CommonDataKinds.Phone._ID + " != ''" ;
Cursor phoneCursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
projection2, where2, null, null);
and then I iterate through each cursor result and getting the columns I want.
The code is :
if (phoneCursor.getCount() > 0) {
while (phoneCursor.moveToNext()) {
String contacts_id = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.Contacts._ID));
String phone_id = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));
Log.i("phonecursor", "contacts_id= " + contacts_id + " phone_id " + phone_id
+ " contacts_name= " + contacts_name + " phone_name= " + phone_name
+ " Phone= " + phone );
}
phoneCursor.close();
}
What I dont get is why Phone.CONTACT_ID is different from the corresponding Contacts._ID from the same row...
Shouldn't both be the same? There are a lot of examples that use those exact columns to run queries. For example here and here if you check the Key pointers.
ContactsContract.Contacts._ID returns unique ID for a row
This is very good example
Image from Get Contact Emails By Content Provider - Android Example
ContactsContract.Contacts._ID returns unique ID for a row.
Now the output will vary based on which cursor you are querying ContactsContract.Contacts._ID.
If you query ContactsContract.Contacts._ID from ContactsContract.Contacts.CONTENT_URI , you will get ContactsContract.Contacts._ID and ContactsContract.CommonDataKinds.Phone.CONTACT_ID same.
But if you query ContactsContract.Contacts._ID from ContactsContract.CommonDataKinds.Phone.CONTENT_URI you will get ContactsContract.Contacts._ID and ContactsContract.CommonDataKinds.Phone.CONTACT_ID different as on each phone entry ContactsContract.Contacts._ID is incremented.
So if you want same _ID and Phone._ID then query from ContactsContract.Contacts.CONTENT_URI instead of ContactsContract.CommonDataKinds.Phone.CONTENT_URI

android sql - how do you order your sql query by multiple columns

I am currently working on an Android project in Eclipse and i am having problems with my SQL query.
I am trying to order the query by more than two columns, currently i am doing it by KEY_DAY_ID but i want to also do it by KEY_START_TIME, but i can't get it to work
my query currently looks like this:
Cursor cursor = db.query(TABLE_SESSION, new String[] {KEY_ID, KEY_MODULE_CODE,
KEY_DAY_OF_WEEK, KEY_START_TIME, KEY_DURATION, KEY_SESSION_TYPE, KEY_ROOM},
null, null, null, null, KEY_DAY_ID + " ASC");
Please let me know your thoughts. Thank you in advance!
The last parameter in db.query() method is the order by clause (without the "order by"). All you need to do is separate both columns by a ",". So it would look like:
Cursor cursor = db.query(TABLE_SESSION, new String[] {KEY_ID, KEY_MODULE_CODE,
KEY_DAY_OF_WEEK, KEY_START_TIME, KEY_DURATION, KEY_SESSION_TYPE, KEY_ROOM},
null, null, null, null, KEY_DAY_ID + " ASC, " + KEY_START_TIME + " ASC");
This works for me
SQLiteCursor cursor = (SQLiteCursor) db.query(DbHelper.TIMES, colmn, null, null, null, null, DbHelper.TABLE_DAY + " ASC, " + DbHelper.TABLE_LECTURE_NO + " ASC",null);
Also you can do it in select line like this:
Cursor data = ddbb.rawQuery("SELECT * FROM vacations ORDER BY NAME ,MONTH , date ",null);
in previous code the first probability for the first column "NAME" then will start arrange by the Second probability "MONTH" then the third "date".....
which mean working in series
Or:
Cursor data = ddbb.rawQuery("select * from vacations where NAME = ? ORDER BY MONTH AND date ",new String[]{ns});
in previous code by using "AND" the two conditions are working together in parallel

Loading phone contacts in ascending order

I am trying to load phone contacts and tried to show the contact names in ascending order. My code is given below:
Cursor cursor = getContentResolver().query(
ContactsContract.Contacts.CONTENT_URI, null,
ContactsContract.Contacts.HAS_PHONE_NUMBER + " = 1", null,
ContactsContract.Contacts.DISPLAY_NAME + " ASC");
I got the required output. But a problem is there, names staring with small letter is shown as last one. First the capital letters are sorted, only after that contact names staring with small letters is shown. PLS HELP
OUTPUT IS:
Alfin A
Bipin B
Calvin C
Jobin
Shine
anurag U
shine H
Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null,
ContactsContract.Contacts.HAS_PHONE_NUMBER + " = 1",
null,
"UPPER(" + ContactsContract.Contacts.DISPLAY_NAME + ") ASC");

Categories

Resources