2014-10-18 72 views
1
public void ReadContacts() { 
    Cursor people = getContentResolver().query(Phone.CONTENT_URI, null, null, null,Phone.DISPLAY_NAME + " ASC "); 
    int indexName = people 
      .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME); 
    int indexNumber = people 
      .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER); 

    people.moveToFirst(); 
    do { 
     name = people.getString(indexName); 
     number = people.getString(indexNumber); 

     contacts.put(name, number); 

    } while (people.moveToNext()); 

    printHashMap(contacts); 

} 

public void printHashMap(HashMap<String, String> a) { 

    for (Entry<String, String> lists : a.entrySet()) { 
     Log.d(lists.getKey(), lists.getValue()); 
    } 

} 

儘管使用ASC,聯繫人還沒有排序嗎?你能幫我解決這個問題嗎? 我用上部()方法也還在Android中排序聯繫人

光標CUR = cr.query(ContactsContract.Contacts.CONTENT_URI,NULL, NULL,NULL, 「上部(」 + ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + 「)ASC」 );

回答

0

將您的(排序的)結果放入HashMap中,該HashMap不保留元素的順序。使用一個列表,它會起作用。

編輯:使用Tuple類,你在你的意見建議,就應該是這樣的:

static class Tuple { 
    public String name; 
    public String number; 

    public Tuple(String name, String number) { 
     this.name = name; 
     this.number = number; 
    } 
} 

public void ReadContacts() { 
    Cursor people = getContentResolver().query(Phone.CONTENT_URI, null, null, null, Phone.DISPLAY_NAME + " ASC "); 
    int indexName = people 
      .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME); 
    int indexNumber = people 
      .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER); 

    List<Tuple> contacts = new ArrayList<Tuple>(); 

    people.moveToFirst(); 
    do { 
     name = people.getString(indexName); 
     number = people.getString(indexNumber); 

     contacts.add(new Tuple(name, number)); 

    } while (people.moveToNext()); 

    printHashMap(contacts); 

} 

public void printList(List<Tuple> list) { 

    for (Tuple tuple : list) { 
     Log.d(tuple.name + ", " + tuple.number); 
    } 
} 

但我建議重命名TupleContact在這種情況下。具有名爲'name'和'number'的成員的元組類是奇怪的。

+0

謝謝。我應該使用ArrayList 來代替? – 2014-10-18 20:10:33

+0

有沒有這樣的事情ArrayList ? – 2014-10-18 20:12:24

+0

虐待必須使用兩個不同的數組列表? – 2014-10-18 20:15:24