【发布时间】:2014-07-11 12:07:53
【问题描述】:
我的自动完成联系建议有什么问题,需要 3-4 秒才能完成加载过程。我的手机里有大约 200 个联系人。该活动用于撰写消息,用户可以在其中键入/搜索联系人并编写消息以发送给收件人。
在我的 oncreate 方法中:
mPeopleList = new ArrayList<Map<String, String>>();
SimpleAdapter mAdapter = new SimpleAdapter(this, mPeopleList, R.layout.custcoview,new String[] { "Name", "Phone", "Type" }, new int[] {R.id.ccontName, R.id.ccontNo, R.id.ccontType });
textView.setThreshold(1);
textView.setAdapter(mAdapter);
PopulatePeopleList();
加载联系人的方法:
public void PopulatePeopleList(){
int i =0;
Cursor people = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
while (people.moveToNext()){
String contactName = people.getString(people.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String contactId = people.getString(people.getColumnIndex(ContactsContract.Contacts._ID));
String hasPhone = people.getString(people.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
if ((Integer.parseInt(hasPhone) > 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));
String numberType = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
Map<String, String> NamePhoneType = new HashMap<String, String>();
NamePhoneType.put("Name", contactName);
NamePhoneType.put("Phone", phoneNumber);
if(numberType.equals("0"))
NamePhoneType.put("Type", "Work");
else
if(numberType.equals("1"))
NamePhoneType.put("Type", "Home");
else if(numberType.equals("2"))
NamePhoneType.put("Type", "Mobile");
else
NamePhoneType.put("Type", "Other");
mPeopleList.add(NamePhoneType); //add this map to the list.
}
phones.close();
}else continue;
}
people.close();
}
编辑
感谢马蒂亚什。这是我现在的工作方法,与上面相比它非常快..
public void readContacts(){
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
int colDisplayName = phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
int colPhoneNumber = phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
int colPhoneType = phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE);
while (phones.moveToNext()) {
String contactName = phones.getString(colDisplayName);
String phoneNumber = phones.getString(colPhoneNumber);
String numberType = phones.getString(colPhoneType);
Map<String, String> NamePhoneType = new HashMap<String, String>();
NamePhoneType.put("Name", contactName);
NamePhoneType.put("Phone", phoneNumber);
if(numberType.equals("0"))
NamePhoneType.put("Type", "Work");
else
if(numberType.equals("1"))
NamePhoneType.put("Type", "Home");
else if(numberType.equals("2"))
NamePhoneType.put("Type", "Mobile");
else
NamePhoneType.put("Type", "Other");
mPeopleList.add(NamePhoneType); //add this map to the list.
}phones.close();
}
【问题讨论】:
-
用户输入字母时速度很慢并且应该出现结果的小部件?发布您拥有的适配器。
-
我在上面编辑了我的工作代码...问题现在解决了..谢谢!
-
感谢更新人!拯救我的一天