我有一個自定義列表視圖,我需要按字母順序顯示聯繫人可以提供示例代碼我如何實現這一目標?如何在列表視圖中按字母順序顯示聯繫人
回答
你可以在alphabatical爲了接觸這樣的:
Cursor cursor = getContentResolver.query(Phone.CONTENT_URI, null, null, null, Phone.DISPLAY_NAME + " ASC");
參考以下鏈接獲取更多cearity:
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
How to get all android contacts but without those which are on SIM
對於列表,你可以參考這個教程:
利用這個代碼來檢索聯繫人
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);
}
});
現在聯繫人詳細信息存儲在聯絡資料 ArrayList中的排列順序。
這工作,但每個聯繫人重複 – Gibs
我已經加入字母頭到聯繫人列表
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 – Gibs
你需要這樣做:
Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");
- 1. xquery組名按字母順序創建聯繫人列表
- 2. 列表視圖按字母順序
- 3. 如何按字母順序排序我的列表視圖?
- 4. 類似於聯繫人列表的jQuery插件按字母順序排列
- 5. Woocommerce如何按字母順序顯示產品列表?
- 6. 如何做聯繫人列表視圖?
- 7. 在Swift的表視圖中按字母順序顯示JSON數據
- 8. 如何使用phonegap從android設備中按字母順序訂購聯繫人?
- 9. 黑莓按字母順序排列顯示列表
- 10. 按字母順序排列並顯示目錄列表PHP
- 11. 抓取聯繫人並將其顯示到列表視圖中
- 12. 按字母順序排序列表視圖
- 13. 如何在Prolog中按字母順序排列列表?
- 14. 可以按字母順序排列Oxyplot圖例中的系列?
- 15. 如何在ASP.NET中按字母順序排序列表?
- 16. 如何在CollectionView中按字母順序顯示名稱
- 17. 在Django-Admin中按字母順序排列顯示項目
- 18. 如何將pList按字母順序拆分成iOS聯繫人等組
- 19. 按jQuery的字母順序顯示導航菜單列表
- 20. 如何顯示我的聯繫人列表中的ParseUser列表
- 21. 按字母順序排序,然後按字母順序排列
- 22. Swiftyjson - JSON到按字母順序排列的表格視圖
- 23. 按字母順序排列單詞列表並按首字母順序篩選
- 24. 在java中按字典順序排列(按字母順序)
- 25. Android。將聯繫人顯示爲列表視圖
- 26. 按字母順序排序列表
- 27. 在Oracle Apex頁面中顯示按字母順序排列的列表
- 28. 如何使用Ionic聯繫人顯示聯繫人圖片
- 29. 如何按字母順序排列組?
- 30. 如何按字母順序排列NSArray?
感謝的人!你真棒:D –