我有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是listview lv_contacts_phone在这里我只得到排序的名字和数字与名称不匹配请帮帮我
答案 0 :(得分:1)
而不是将contactNumber
和contactName
人存储在单独的List
中。您可以创建一个封装Person
和contactName
的类contactNumber
。填充List<Person>
然后对其进行排序。或者,使用可以使用Map<String,String>
,其中密钥为contactNumber
,并为contactName
值。
答案 1 :(得分: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;
}
}
现在联系人已对数据进行了排序,您可以在适配器中进行设置。
答案 2 :(得分: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);
}