Listen findet man in allen Android Apps. Sie sind ein gutes Mittel, Informationen schnell zugänglich darzustellen und Anleitungen für Android, Scrollbare Listen zu erstellen gibt es schon viele. Doch ich konnte keine so schnell finden, die mir einfach und kurz meinen Anwendungsfall erklärt.
Was wir hier machen
- Eine List-View in eine bestehende Activity einfügen
- Ein eigenes Row-Layout anlegen
- Einen eigenen ArrayAdapter erstellen
- Den ArrayAdapter mit der View verbinden
[ccNe_xml]
[/ccNe_xml]
[ccNe_xml]
[/ccNe_xml]
[ccN_java]
public class Contact {
private String displayName;
public Contact(String displayName) {
this.displayName = displayName;
}
public String getDisplayName() {
return displayName;
}
public void setDisplayName(String displayName) {
this.displayName = displayName;
}
}
[/ccN_java]
Und jetzt kommt noch der Adapter, den wir verwenden, um ihn an die ListView zu hängen.
[ccN_java]
public class InteractiveArrayAdapterContact extends ArrayAdapter {
private List contactList;
private Activity context;
public InteractiveArrayAdapterContact(Activity context,
List contactList) {
super(context, R.layout.contact_row_layout, contactList);
this.context = context;
this.contactList = contactList;
}
static class ViewHelper {
protected TextView text;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
if (convertView == null) {
LayoutInflater inflator = context.getLayoutInflater();
view = inflator.inflate(R.layout.contact_row_layout, null);
ViewHelper vHelper = new ViewHelper();
vHelper.text = (TextView) view.findViewById(R.id.label);
view.setTag(vHelper);
} else {
view = convertView;
}
ViewHelper holder = (ViewHelper) view.getTag();
holder.text.setText(contactList.get(position).getDisplayName());
return view;
}
}
[/ccN_java]
Jetzt können wir alles zusammensetzen und in der Aktivity mit der View verknüpfen.
[ccN_java]
public class AddGroupActivity extends Activity {
private List contactList;
private ArrayAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_group);
ListView lv = (ListView) findViewById(R.id.listView1);
adapter = new InteractiveArrayAdapterContact(this, getContacts());
lv.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_add_group, menu);
return true;
}
public void showAddContact(View view){
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, /* request Code Integer */ 25);
}
@Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
switch(reqCode){
case(25):
if (resultCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
Cursor c = managedQuery(contactData, null, null, null, null);
if (c.moveToFirst()) {
String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
adapter.add(new Contact(name));
}
}
}
}
private List getContacts() {
return contactList = new ArrayList();
}
}
[/ccN_java]
[cce_xml highlight=”7″]
[/cce_xml]