2013-06-19 60 views
2

我有2個列表,其中一個是名稱,另一個是數字。當我有點名稱列出其工作正常,但數量不與這些名字相匹配,這是我的代碼如何在android中一次對2個列表進行排序

try { 
     Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI; 
     String[] projection = new String[] { 
       ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, 
       ContactsContract.CommonDataKinds.Phone.NUMBER }; 
     List<String> contactNames = new ArrayList<String>(); 
     List<String> contactNumbers = new ArrayList<String>(); 
     Cursor people = getContentResolver().query(uri, projection, null, 
       null, null); 

     int indexName = people 
       .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME); 
     int indexNumber = people 
       .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER); 

     people.moveToFirst(); 
     do { 
      String name = people.getString(indexName); 
      String number = people.getString(indexNumber); 
      contactNames.add(name); 
      contactNumbers.add(number); 
     } while (people.moveToNext()); 
     PhoneContactsAdapter adapter = new PhoneContactsAdapter(context, 
       contactNames, contactNumbers, selectedContacts); 
     Collections.sort(contactNames); 
     adapter.notifyDataSetChanged(); 
     lv_contacts_phone.setAdapter(adapter); 
     lv_contacts_phone.setFastScrollEnabled(true); 
    } catch (Exception e) { 
     System.out 
       .println("(SELECTFRIENDSACTIVITY)Selecting contacts from friends: " 
         + e); 
    } 

這裏contactNames和contactNumbers被givent這裏的列表視圖lv_contacts_phone我只得到排序的名字和號碼不正確匹配與名稱請幫我

+0

嘗試使用Collection.sort(名單); – abhi

+0

@新白癡是正確的,使用自定義類,而不是排序它。請參閱此鏈接http://stackoverflow.com/questions/10091110/sorting-custom-class-array-list-string-using-collections-sort/10091403#10091403 –

+0

@Hiren Dabhi在您的鏈接只使用一個列表。所以他可以分類聯繫人的姓名。但在我的代碼中,我想排序名稱以及代碼 – sarabu

回答

1

而不是存儲contactNumbercontactName的人在單獨List s。您可以創建一個Person類,它封裝了contactNamecontactNumber。填入List<Person>然後對其進行排序。或者使用可以使用Map<String,String>,其中密鑰將是contactNumber並且值contactName

+0

在地圖<字符串,字符串>我們不能保留列表,我們只保留字符串 – sarabu

0

使用此代碼按名稱對聯繫人進行排序。

try { 
     Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI; 
     String[] projection = new String[] { 
       ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, 
       ContactsContract.CommonDataKinds.Phone.NUMBER }; 
     List<Person> contacts = new ArrayList<Person>(); 
     Cursor people = getContentResolver().query(uri, projection, null, 
       null, null); 

     int indexName = people 
       .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME); 
     int indexNumber = people 
       .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER); 

     people.moveToFirst(); 
     do { 
      Person person = new Person(); 
      String name = people.getString(indexName); 
      String number = people.getString(indexNumber); 
      person.setName(name); 
      person.setNumber(number); 
      contacts.add(person); 
      Collections.sort(contacts, new ContactsComparator()); 
     } while (people.moveToNext()); 

    } catch (Exception e) { 
     System.out 
       .println("(SELECTFRIENDSACTIVITY)Selecting contacts from friends: " 
         + e); 
    } 

這裏是比較類:

class ContactsComparator implements Comparator<Person> { 
    public int compare(Person p1, Person p2) {   
     return p1.getName().compareTo(p1.getName()); 
    } 
} 

數據(人)類:

public class Person { 
private String name; 
private String number; 
public String getName() { 
    return name; 
} 
public void setName(String name) { 
    this.name = name; 
} 
public String getNumber() { 
    return number; 
} 
public void setNumber(String number) { 
    this.number = number; 
} 

}

現在的聯繫人排序的數據,你可以設置適配器。

+0

是它爲我工作。非常感謝你 – sarabu

0

我得到了一個很好的解決方案

try { 
     Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI; 
     String[] projection = new String[] { 
       ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, 
       ContactsContract.CommonDataKinds.Phone.NUMBER }; 
     List<String> contactNames = new ArrayList<String>(); 
     List<String> contactNumbers = new ArrayList<String>(); 
     Cursor people = getContentResolver().query(uri, projection, null, 
       null, null); 
     int indexName = people 
       .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME); 
     int indexNumber = people 
       .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER); 
     Map<String, String> mapStrings = new HashMap<String, String>(); 
     people.moveToFirst(); 
     do { 
      String name = people.getString(indexName); 
      String number = people.getString(indexNumber); 
      mapStrings.put(name, number); 
     } while (people.moveToNext()); 
     Map<String, String> sortedMap = new TreeMap<String, String>(
       mapStrings); 
     for (Entry<String, String> entry : sortedMap.entrySet()) { 
      contactNames.add(entry.getKey()); 
      contactNumbers.add(entry.getValue()); 
     } 

     lv_contacts_phone.setAdapter(new PhoneContactsAdapter(context, 
       contactNames, contactNumbers, selectedContacts)); 
     lv_contacts_phone.setFastScrollEnabled(true); 

    } catch (Exception e) { 
     System.out 
       .println("(SELECTFRIENDSACTIVITY)Selecting contacts from friends: " 
         + e); 
    } 
相關問題