【问题标题】:Load contacts in android in sorting order按排序顺序在android中加载联系人
【发布时间】:2013-09-24 20:18:59
【问题描述】:

我想将联系人从手机加载到我的应用中。这段代码完美无缺,只是它不返回排序列表。 e-d Abid 012345678 将在列表的顶部,但 abid 012345678 将在最后。我尝试过不同的光标组合(如您在 // cmets 中所见)。寻求您的指导..

List<ContactInfo> LoadContactListFromPhone()
{

    Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, "UPPER(" + ContactsContract.Contacts.DISPLAY_NAME + ") ASC");
    //Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, ContactsContract.Contacts.HAS_PHONE_NUMBER + " = 1",  null, "UPPER(" + ContactsContract.Contacts.DISPLAY_NAME + ") ASC");
    //Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null); 
    if(cursor.moveToFirst())
    {
        while (cursor.moveToNext()) 
        { 
            String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)); 
            String hasPhone =  cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
            int hasph = Integer.parseInt(hasPhone);

            if (hasph>-1)
            { 
                Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,   ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId, null, null); 
                if(phones.getCount() > 0)
                {
                    while (phones.moveToNext())
                    { 
                        String phoneNumber = phones.getString(phones.getColumnIndex( ContactsContract.CommonDataKinds.Phone.NUMBER)); 
                        String phonename = phones.getString(phones.getColumnIndex( ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                        list.add(new ContactInfo(phonename, 0, phoneNumber,0));
                    }
                }
                phones.close(); 
            }
        }
        myList = list;
    }
    else
    {
        Toast.makeText(this, "No Contact Found",Toast.LENGTH_LONG).show();
    }
    cursor.close(); 
    return myList;
}

【问题讨论】:

  • 有没有人知道这段代码有什么问题...或者您是否可以提供一些指导或代码...

标签: android cursor android-contacts android-contentresolver


【解决方案1】:

试试下面的代码

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

对我来说很好用

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多