Sorting order music player android - android

ContentResolver contentResolver = context.getContentResolver();
Uri uri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Cursor cursor = contentResolver.query(uri, null, selection, null, MediaStore.Audio.Media.TITLE + " ASC");
I want to sort audio by title name but running the above code ,i get capital letter audio first and then all the small letter audio.
how do i get absolute sort order,not mixed ordered list?
like from A to z

Try with
Cursor cursor = contentResolver.query(uri, null, selection, null, MediaStore.Audio.Media.TITLE + " COLLATE NOCASE ASC");
See if this works. I have not tried it, Just read somewhere long time back.

Related

Search Phone numbers from Android Contact Database

I am implementing an AutocompleteView to search phone numbers. The code is working fine except in some conditions.
My Code :
Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
String[] projection = new String[]{ContactsContract.CommonDataKinds.Phone._ID, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone.TYPE};
String selection = ContactsContract.CommonDataKinds.Phone.NUMBER + " LIKE ?";
String[] selectionArgs = new String[]{"%" + charSequence.toString() + "%"};
Cursor cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");
This code is working fine where there is no space in the phone numbers. For example if i enter '123' in my autocompleteView, it is able to find phone numbers like '9123456789' or '8283929383' but it is not able to find numbers '9123 456 789' or '912 3456 789'
I even tried implementing this with ContactsContract.PhoneLookup API but with this, it didnt work at all.
Code with ContactsContract.PhoneLookup API:
String[] projection = new String[]{ContactsContract.PhoneLookup._ID, ContactsContract.PhoneLookup.DISPLAY_NAME, ContactsContract.PhoneLookup.NUMBER, ContactsContract.PhoneLookup.TYPE};
Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(charSequence.toString()));
Cursor cursor = context.getContentResolver().query(uri, projection, null, null, ContactsContract.PhoneLookup.DISPLAY_NAME + " ASC");
Any help is appreciated.
Instead of using ContactsContract.CommonDataKinds.Phone.NUMBER, use ContactsContract.CommonDataKinds.Phone.NORMALIZED_NUMBER (this return phone numbers in E164 format)

Android : Contact List is not by all case of Letters

I am trying to fetch all Contact list in a sorting order. I am getting all the contact list but its first sort name starts with Upper case (ABCD..), After sorting all the name starts with Upper case letter then only it start to sort remaining name that starts with Lower case letter (a,b,c....)
private static final String[] PROJECTION = new String[] {
ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER
};
ContentResolver cr = getContentResolver();
Cursor cursor = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, PROJECTION,
null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");
if (cursor != null) {
try {
final int nameIndex =
cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
nameContact = cursor.getString(nameIndex);
finally {
cursor.close();
}
Here the name is sorting as a separate two list. First sort all the name start with Upper Case after that sort name start with Lower Case.
Can any one please tell me how to solve this issue?
Thanks in advance :)
Use the query like this with collate.
Cursor cursor = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, PROJECTION,
null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " COLLATE NOCASE ASC");

Empty cursor row when filtering with selection

I'm not confident with cursors and I'm facing some problem when filtering one with a WHERE clause.
What I'm doing:
ContentResolver contentResolver = context.getContentResolver();
Uri uriConversation = Uri.parse("content://mms-sms/conversations/");
String[] projection = new String[]{"*"};
String selection = "address=" + phoneNumberForThread;
Cursor cursor = contentResolver.query(uriConversation, projection, null, null, null);
Executing this code the cursor get filled and works perfectly.
However, if I swap the null selection argument with my selection String as
Cursor cursor = contentResolver.query(uriConversation, projection, selection, null, null);
I then get an empty cursor. I even check for !phoneNumberForThread.isEmpty()
I think I'm doing something wrong but again I am not confident with cursor yet.
Any help would be really appreciated.
If your input variabel is stored as a string data type, you should enclose it with ' as such:
String selection = "address='" + phoneNumberForThread + "'";

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

sql where clause for managedQuery android method

I am trying to write the parameters to the managedQuery method in android and having trouble with the WHERE clause, here is what i wrote;
Cursor imageCursor;
// lots of other junk
String[] proj = {MediaStore.Images.Media.TITLE};
imageCursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, proj, MediaStore.Images.Media.DATA = filename, proj, null );
"filename" is a string variable holding the path of an image, example /mnt/sdcard/pic05.png
I want it to return a cursor holding a record that is the same record for the pic05.png image and the cursor returned would hold the TITLE column information for this specific picture. How did i mess up the sql WHERE CLAUSE? I thought that my syntax must be wroing on the where clause
Add ' ' around filename. Try this
Cursor imageCursor;
// lots of other junk
String[] proj = {MediaStore.Images.Media.TITLE};
String selection = MediaStore.Images.Media.DATA + "='" + filename +"'";
imageCursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, proj, selection, null, null );

Categories

Resources