我想将联系人从手机加载到我的应用程序。这段代码完美无缺,只是它不返回排序列表。ed Abid 012345678 将在列表的顶部,但 abid 012345678 将在最后。我尝试了不同的光标组合(如您在 // 评论中所见)。寻求您的指导。。
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;
}