如何在Listview中按字母顺序显示联系人[关闭]
Posted
技术标签:
【中文标题】如何在Listview中按字母顺序显示联系人[关闭]【英文标题】:How to display Contacts in alphabetic order in Listview [closed] 【发布时间】:2011-11-11 02:50:53 【问题描述】:我有一个自定义列表视图,我需要按字母顺序显示联系人,你能提供示例代码如何实现吗?
【问题讨论】:
【参考方案1】:您可以像这样按字母顺序获取联系人:
Cursor cursor = getContentResolver.query(Phone.CONTENT_URI, null, null, null, Phone.DISPLAY_NAME + " ASC");
请参考这些链接以获得更多信息:
How to call android contacts list?
How to get contacts from native phonebook in android
How to obtain all details of a contact in Android
How to get the first name and last name from Android contacts?
How to import contacts from phonebook to our application
Android contacts extraction
How to get all android contacts but without those which are on SIM
列表可以参考本教程:
http://www.vogella.de/articles/AndroidListView/article.html
【讨论】:
谢谢你!你太棒了:D【参考方案2】:你需要这样做:
Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");
【讨论】:
谢谢。我想知道为什么问题所有者不接受它【参考方案3】:使用此代码检索联系人
ArrayList<HashMap<String,String>> contactData=new ArrayList<HashMap<String,String>>();
ContentResolver cr = getContentResolver();
Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
while (cursor.moveToNext())
try
String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
String name=cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
if (Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
Cursor phones = getContentResolver().query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId, null, null);
while (phones.moveToNext())
String phoneNumber = phones.getString(phones.getColumnIndex( ContactsContract.CommonDataKinds.Phone.NUMBER));
HashMap<String,String> map=new HashMap<String,String>();
map.put("name", name);
map.put("number", phoneNumber);
contactData.add(map);
phones.close();
catch(Exception e)
现在联系人详细信息存储在 contactData ArrayList 中。现在像下面这样排序这个arrayList
Collections.sort(contactData, new Comparator()
@Override
public int compare(Object o1, Object o2)
HashMap map1=(HashMap)o1;
HashMap map2=(HashMap)o2;
String s1=(String)map1.get("name");
String s2=(String)map2.get("name");
return s1.compareTo(s2);
);
现在联系人详细信息按排序顺序存储在 contactData ArrayList 中。
【讨论】:
这行得通,但每个联系人都有重复项【参考方案4】:我已将字母标题添加到联系人列表中
new ReadContactTask(context, showProgressDialog,new OnContactReadListener()
@Override
public void onContactRead(List<ContactItem> dummyList)
ArrayList<ContactItem> latestPhoneContacts = new ArrayList<>();
if(dummyList!=null && dummyList.size()>0)
tvNoResult.setVisibility(View.GONE);
for(ContactItem contactItem : dummyList)
String contactNameInCapital = "";
if(contactItem.getContactName()!=null && contactItem.getContactName().length()>2)
contactNameInCapital = contactItem.getContactName().substring(0,1).toUpperCase();
if(contactNameInCapital.charAt(0) != previousContact.charAt(0))
latestPhoneContacts.add(new ContactItem(contactNameInCapital.substring(0,1)));
latestPhoneContacts.add(contactItem);
else
latestPhoneContacts.add(contactItem);
previousContact = contactNameInCapital;
if(latestPhoneContacts!=null && latestPhoneContacts.size()>0)
contactListAdapter.addItems(latestPhoneContacts);
@Override
public void onContactEmpty()
).execute();
在哪里
private String previousContact ="Zee";
【讨论】:
什么是 ContactItem以上是关于如何在Listview中按字母顺序显示联系人[关闭]的主要内容,如果未能解决你的问题,请参考以下文章
在 Android 中按字母顺序对 Hashmap 值类型进行排序
如何在 swift 中按字母顺序对 JSON 字符串进行排序?