[英]Optimisation for reading contacts in listView?
I have code to read contacts, get name and number and put them in listView. 我有读取联系人,获取姓名和电话号码并将其放入listView的代码。 Everything works but application read all contacts in the same moment. 一切正常,但应用程序同时读取所有联系人。 I guess that I have to read them on scroll move, but I don't now how, please help? 我想我必须滚动阅读它们,但是现在不知道如何了,请帮忙吗? Thank you in advance, Wolf. 预先谢谢你,沃尔夫。
Here is my code: 这是我的代码:
ArrayList> mapa = new ArrayList>(); ArrayList> mapa = new ArrayList>();
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
if(cur.getCount() > 0){
while(cur.moveToNext()){
id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
if(Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0){
final Cursor numCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]{id}, null);
for(numCur.moveToFirst(); !numCur.isAfterLast(); numCur.moveToNext()){
brTel = numCur.getString(numCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
ime = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
tmpIme = new String[] {ime};
for(int i = 0; i < tmpIme.length; i++){
HashMap<String, String> imeMapa = new HashMap<String, String>();
imeMapa.put("imeLista", ime);
imeMapa.put("Mobilni", brTel);
mapa.add(imeMapa);
}
}
numCur.close();
}
} // While
}
SimpleAdapter sa = new SimpleAdapter(getApplicationContext(), mapa, R.layout.imenik, new String[] {"imeLista", "Mobilni"}, new int[] {R.id.tvImeImenik, R.id.tvSamoProba});
lImenik.setAdapter(sa);
I think you should use a SimpleCursorAdapter, after that the application should be fast enough. 我认为您应该使用SimpleCursorAdapter,之后应用程序应该足够快。 Or did I misunderstand? 还是我误会了?
@Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
final Cursor c = managedQuery(Contacts.CONTENT_URI,
new String[]{Contacts._ID, Contacts.DISPLAY_NAME},
Contacts.DISPLAY_NAME + " != ?", new String[] {"NULL"},
Contacts.DISPLAY_NAME );
String[] from = new String[] { android.provider.ContactsContract.Contacts.DISPLAY_NAME };
int[] to = new int[] { R.id.nativecontacts_entry_name };
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.rawlayout, c, from, to);
setListAdapter(adapter);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.