2014-10-18 78 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