Hallo,
ich möchte in einer Android App alle Kontakte auslesen und einer Liste (als Alert) darstellen.
Das klappt mit nachfolgendem Code auch soweit. Allerdings ist der absolut ineffizient. Er lädt da selbst bei einem Top Gerät eine halbe Ewigkeit für die (wenigen) Kontakte.
Außerdem werden mir alle mehrfach angezeigt.
Auch die Darstellung mit einem Umweg über eine LinkedList ist ziemlich suboptimal. Aber ich habe absolut keine Idee wie man es anders lösen könnte. Weiß da jemand Rat ?
ich möchte in einer Android App alle Kontakte auslesen und einer Liste (als Alert) darstellen.
Das klappt mit nachfolgendem Code auch soweit. Allerdings ist der absolut ineffizient. Er lädt da selbst bei einem Top Gerät eine halbe Ewigkeit für die (wenigen) Kontakte.
Außerdem werden mir alle mehrfach angezeigt.
Auch die Darstellung mit einem Umweg über eine LinkedList ist ziemlich suboptimal. Aber ich habe absolut keine Idee wie man es anders lösen könnte. Weiß da jemand Rat ?
Java:
public void contactPicker(View view) {
if(list.isEmpty()){
Cursor rawContacts = managedQuery(RawContacts.CONTENT_URI, projection, null, null, null);
if(rawContacts.moveToFirst()){
while(!rawContacts.isAfterLast()){
int contactId = rawContacts.getInt(rawContacts.getColumnIndex(RawContacts.CONTACT_ID));
Cursor contact = managedQuery(Contacts.CONTENT_URI, contact_value, Contacts._ID + "=?", new String[]{String.valueOf(contactId)}, null);
if(contact.moveToFirst()){
Cursor phone = managedQuery(Phone.CONTENT_URI, contact_phone, Data.CONTACT_ID + "=?",new String[]{String.valueOf(contactId)}, null);
if(phone.moveToFirst()) {
while(!phone.isAfterLast()){
list.add(new Kontakt(contact.getString(contact.getColumnIndex(Contacts.DISPLAY_NAME)), phone.getString(phone.getColumnIndex(Phone.NUMBER))));
//message(contact.getString(contact.getColumnIndex(Contacts.DISPLAY_NAME)) + " " + phone.getString(phone.getColumnIndex(Phone.NUMBER)));
phone.moveToNext();
}
}
phone.close();
contact.close();
}
rawContacts.moveToNext();
}
}
CharSequence[] cs = new CharSequence[list.size()];
int i = 0;
for(Kontakt k : list){
cs[i] = k.getName();
i++;
}
builder = new AlertDialog.Builder(this);
builder.setTitle("Kontakte");
builder.setCancelable(true);
builder.setItems(cs, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
setContact(list.get(item));
}
});
alert = builder.create();
}
alert.show();
}