Loading phone contacts in ascending order - android

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");

Related

Android: Show all contacts sorted by favorites

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");

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

Retrieve a list of contacts having phone number like the dialed number

I'm trying to retrieve the phone contacts having phone number starting with the number being dialled. For eample if I type 123, I would like to retrieve all the contacts having contact number starting with 123. I'm using the following code for this:
Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
Cursor cursor = this.getContentResolver().query(
uri,
new String[] { ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME },
ContactsContract.CommonDataKinds.Phone.NUMBER + " LIKE '" + dialledNumber + "%'", null,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");
The issue with this code is, if I have saved a contact like +919-9.... and another like +9199...., when I dial +9199 I can't retrieve both the contacts. I would like to escape the character "-" while querying the contacts. How could I do this? Please help. Thank you.
System.out.print("1-2-3".replaceAll("\\-", ""));
"My problem is not with the dialled number. Even if I dial 1234 or 123-4, I need all the contacts with phone number starting with 1234. But here if I type 1234 only contacts starting with 1234 is retrieved and not 123-4."
that is because you directly check for the number.try making your string dialledNumber such that it looks like 123-456-7890 then query for both this string like:
If suppose,
String dialledNumber="1234";
String dialledNumberFormatted="123-4"; // Create this on your own from dialledNumber you get
Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
Cursor cursor = this.getContentResolver().query(
uri,
new String[] { ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME },
ContactsContract.CommonDataKinds.Phone.NUMBER + " LIKE '" + dialledNumber + "%'" +" OR "+ContactsContract.CommonDataKinds.Phone.NUMBER + " LIKE '" + dialledNumberFormatted + "%'", null,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");
This is a type of hack but you can use it doesn't cause problem for you anywhere else.

Select first n rows in contentProvider

I've this select:
final Cursor cursorConversations = getContentResolver().query(
Uri.parse("content://gmail-ls/conversations/" + Uri.encode(mailAddress)),
null, null, null, BaseColumns._ID + " DESC");
ContentQueryMap mQueryMap
= new ContentQueryMap(cursorConversations, BaseColumns._ID, true, null);
With ContentQueyMap I can cache Cursor data and iterate in it also with the Cursor closed (i need it to boost performance).
Now, I want that the select of the Corsor only retrieve the first fifty rows. The solution of looping for 50 times in mQueryMap.getRows().entrySet() is not right: I don't want that mQueryMap gets all the rows of the Cursor, but only the first fifty.
Any idea? Does exist a where clause to get only first n rows?
You could do
final Cursor cursorConversations = getContentResolver().query(
Uri.parse("content://gmail-ls/conversations/" + Uri.encode(mailAddress)),
null, null, null, BaseColumns._ID + " DESC " + " LIMIT 5");
"LIMIT x" after your SORT.
Cheers
SELECT * FROM Table_Name LIMIT 5;
That's valid for sqlite. Try adding "LIMIT 5" to your where clause. (I have not tried)

Categories

Resources