我有一個自定義列表視圖,我需要按字母順序顯示聯繫人可以提供示例代碼我如何實現這一目標?如何在列表視圖中按字母順序顯示聯繫人
回答
你可以在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. 聯繫人列表不是按照字母順序在列表視圖
- 2. 按字母順序排列聯繫人列表清單
- 3. xquery組名按字母順序創建聯繫人列表
- 4. 列表視圖按字母順序
- 5. 按姓氏排列聯繫人以按字母順序排列的列表顯示
- 6. 按字母順序排列的聯繫人
- 7. 在android中按字母順序獲取聯繫人?
- 8. 如何按字母順序排序我的列表視圖?
- 9. 如何按字母順序排列通訊錄中的聯繫人?
- 10. 如何在使用SwiftAddressBook時按字母順序排列聯繫人?
- 11. 按字母順序顯示列表框中顯示的內容
- 12. 類似於聯繫人列表的jQuery插件按字母順序排列
- 13. Woocommerce如何按字母順序顯示產品列表?
- 14. 如何按列主顯示順序顯示一系列數字?
- 15. Rails的顯示錶按字母順序
- 16. 獲取所有聯繫人並在列表視圖中顯示
- 17. 如何做聯繫人列表視圖?
- 18. 在字母表中顯示按字母順序排列的AR對象數組
- 19. 如何使用phonegap從android設備中按字母順序訂購聯繫人?
- 20. 在Swift的表視圖中按字母順序顯示JSON數據
- 21. 黑莓按字母順序排列顯示列表
- 22. 按字母順序排列並顯示目錄列表PHP
- 23. 抓取聯繫人並將其顯示到列表視圖中
- 24. 如何在表格視圖中將數組按字母順序排列組織
- 25. 按字母順序排序列表視圖
- 26. 按字母順序排序JQM列表視圖?
- 27. Android按字母順序排序列表視圖
- 28. 如何在Prolog中按字母順序排列列表?
- 29. 如何在ASP.NET中按字母順序排序列表?
- 30. 可以按字母順序排列Oxyplot圖例中的系列?
感謝的人!你真棒:D –