Javier
Javier

Reputation: 1685

Order by not working in android cursor

I had some cursors ti retrieve contacts from Android, all its ok but the problem its i cant make work the "Order by" clause. i tried a lot of things, even using "Upper" and localized collection clauses but seems isnt working, im using a hashmap to store the results, so i guess i will need order the hashmap but i dont want this because order by should solve the problem and save processing time. This is my code:

 private Cursor getContacts() {
        // Run query
        Uri uri = ContactsContract.Contacts.CONTENT_URI;
        String[] projection = new String[] { ContactsContract.Contacts._ID,
            ContactsContract.Contacts.DISPLAY_NAME };
        String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '"
            + ("1") + "' AND "+ ContactsContract.Contacts.HAS_PHONE_NUMBER +"='" +("1")+"'";
        String sortOrder =" "+  ContactsContract.Contacts.DISPLAY_NAME +" ASC";

        CursorLoader query = new CursorLoader(context,uri, projection,
                selection, null,
            sortOrder);
        return query.loadInBackground();
      }

and this is other to get contacs by group:

contactInfo = new HashMap<String, ContactInfo>();
         Cursor c = context.getApplicationContext().getContentResolver().query(ContactsContract.Data.CONTENT_URI
                , new String[] {
                ContactsContract.Contacts.DISPLAY_NAME, ContactsContract.Contacts._ID, 
                ContactsContract.RawContacts.CONTACT_ID },
                ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID + " = ?"+ " AND "
                + ContactsContract.Contacts.HAS_PHONE_NUMBER +"='" +("1")+"'",
                new String[] { groupId },"UPPER("+ ContactsContract.Contacts.DISPLAY_NAME + ") ASC");

after getting cursor i use the while(c.moveToNext()) code

Upvotes: 1

Views: 1813

Answers (1)

Kartihkraj Duraisamy
Kartihkraj Duraisamy

Reputation: 3221

Got it worked ,

context.getContentResolver().query(CONTENT_URI, null, buf == null ? null : buf.toString(),args,Cons.ATTRIBUTES_NAME + " ASC");

Without have a space in the orederby string,

=" "+  ContactsContract.Contacts.DISPLAY_NAME +" ASC"

From the above line remove the space before the attribute name.

Upvotes: 1

Related Questions