2016-03-08 72 views
1

使用cursorLoader人的名字,我有什麼: 我有一個光標,但所有聯繫都是隨機升序排列顯示聯繫人的android系統

我想實現: 我想將聯繫人按照姓名的升序排列。


什麼,我需要指定:

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

回答

1

你可以嘗試:

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