0

モバイルから自分のアプリに連絡先を読み込みたいです。このコードは、ソートされたリストを返さないことを除けば問題なく動作します。ed Abid 012345678 はリストの一番上になりますが、abid 012345678 は最後になります。Cursor のさまざまな組み合わせを試しました (// コメントでわかるように)。あなたのガイダンスを探しています..

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

1 に答える 1