使用android

时间:2016-03-08 13:34:34

标签: android android-cursorloader

我有什么: 我有一个光标,但所有的联系人都是随机的

我想要实现的目标: 我想让一个带有联系人的游标按升序排列到该人的姓名。

我需要指定的内容:

 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 个答案:

答案 0 :(得分:3)

你可以尝试:

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