Search code examples
androidandroid-cursorloader

Displaying contacts in ascending order to the name of person using cursorLoader in android


What I have: I have a cursor but all the contacts are randomly

What I am trying to achieve: I want to have a cursor with contacts in ascending order to the name of the person.


What i need to specify in:

 String selection = null;                                 //Selection criteria
 String[] selectionArgs = {};                             //Selection criteria
 String sortOrder = null;                                 //The sort order for the returned rows

@Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        // This is called when a new Loader needs to be created.
        pd= new ProgressDialog(ActHome.this);
        pd.setMessage("loading...");
        pd.show();
        if (id == CONTACTS_LOADER_ID) {
            return contactsLoader();
        }
        return null;
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
        //The framework will take care of closing the
        // old cursor once we return.
        pd.dismiss();
        List<String> contacts = contactsFromCursor(cursor);
        /*for(int i=0;i<contacts.size();i++){
            Toast.makeText(getApplicationContext(), contacts.get(i), Toast.LENGTH_SHORT).show();
        }*/
        //String mMsg=getResources().getString(R.string.settings_content);

        //CommonFunctions.inviteAllPeople(ActHome.this,contacts,mMsg,names);


    }

    @Override
    public void onLoaderReset(Loader<Cursor> loader) {
        // This is called when the last Cursor provided to onLoadFinished()
        // above is about to be closed.  We need to make sure we are no
        // longer using it.
    }

    private  Loader<Cursor> contactsLoader() {
        Uri contactsUri = ContactsContract.Contacts.CONTENT_URI; // The content URI of the phone contacts

        String[] projection = {                                  // The columns to return for each row
                ContactsContract.Contacts.DISPLAY_NAME
        } ;

        String selection = null;                                 //Selection criteria
        String[] selectionArgs = {};                             //Selection criteria
        String sortOrder = null;                                 //The sort order for the returned rows

        return new CursorLoader(
                getApplicationContext(),
                contactsUri,
                projection,
                selection,
                selectionArgs,
                sortOrder);
    }

    private List<String> contactsFromCursor(Cursor cursor) {

        contacts = new ArrayList<String>();
        names = new ArrayList<String>();

        Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
        while (phones.moveToNext())
        {
          String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
          String name = phones.getString(phones.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
          syncTheName(name.replaceAll("\\s",""));
          syncTheNumber(phoneNumber.replaceAll("\\s",""));
        }
        phones.close();
        return contacts;
    }

Solution

  • Can you try:

    Cursor cursor =  getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
                   null, 
                   null,
                   null,
                   ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME+" ASC");