EditText und darunter ListView für Kontaktliste

  • Antworten:2
Wicki12
  • Forum-Beiträge: 38

24.05.2012, 19:03:46 via Website

Hallo,

ich möchte die Kontaktliste meines Geräts anzeigen und über einen Filter die Daten reduzieren, das funktioniert auch mit der im Code angegebenen ListActivity. Diese Activity möchte ich nun so erweitern, dass über der ListView ein "EditText" angezeigt wird, wo der Filtertext einzugeben ist.
Ich weiss nun nicht, wie ich die Listenelemente der ListView aus meiner main.xml zuordnen kann.
Bitte um Hilfe.

1import android.app.ListActivity;
2import android.content.Intent;
3import android.database.Cursor;
4import android.database.CursorWrapper;
5import android.net.Uri;
6import android.os.Bundle;
7import android.provider.ContactsContract;
8import android.provider.ContactsContract.Contacts;
9import android.view.View;
10import android.widget.AdapterView;
11import android.widget.AdapterView.OnItemClickListener;
12import android.widget.EditText;
13import android.widget.ListAdapter;
14import android.widget.ListView;
15import android.widget.SimpleCursorAdapter;
16
17public class MiniContacts extends ListActivity {
18
19 private Cursor cursorContacts;
20
21 @Override
22 public void onCreate(Bundle savedInstanceState) {
23 super.onCreate(savedInstanceState);
24// setContentView(R.layout.main);
25// EditText ed = (EditText)findViewById(R.id.editText1);
26 // Kontaktliste ermitteln
27 String filter="display_name like 'xyz%'";
28 cursorContacts = getContentResolver().query(
29 ContactsContract.Contacts.CONTENT_URI, null, filter, null, null);
30 startManagingCursor(cursorContacts);
31 // bildet Datensätze auf Listenelemente ab
32 ListAdapter adapter = new SimpleCursorAdapter(this,
33 android.R.layout.simple_list_item_1, cursorContacts,
34 new String[] { Contacts.DISPLAY_NAME },
35 new int[] { android.R.id.text1 });
36 setListAdapter(adapter);
37 getListView().setOnItemClickListener(new OnItemClickListener() {
38
39 @Override
40 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
41 Object o = getListAdapter().getItem(position);
42 if (o instanceof CursorWrapper) {
43 // Welcher Datensatz?
44 CursorWrapper w = (CursorWrapper) o;
45 int columnIndex = w.getColumnIndex(Contacts._ID);
46 long contactId = w.getLong(columnIndex);
47 Uri uri = Uri.withAppendedPath(
48 ContactsContract.Contacts.CONTENT_URI,
49 Long.toString(contactId));
50 // Kontakt anzeigen
51 Intent intent = new Intent(Intent.ACTION_VIEW, uri);
52 startActivity(intent);
53 }
54 }
55 });
56
57 }
58
59 @Override
60 protected void onDestroy() {
61 super.onDestroy();
62 stopManagingCursor(cursorContacts);
63 }
64}

Gruss Wicki

Antworten
Ansgar M
  • Forum-Beiträge: 1.544

24.05.2012, 21:05:56 via Website

Ich verstehe dein Problem nicht richtig, aber:

Wenn du eine ListActivity mit einem eigenem Layout verwendest, brauchst du in diesem Layout eine ListView mit der Id android:list. Sonst stürzt die App mit entsprechender Fehlermeldung ab.

Lg Ansgar

Antworten
Wicki12
  • Forum-Beiträge: 38

24.05.2012, 21:51:14 via Website

Hallo,

Dank für den Hinweis. Momentan kann ich im eigenen Layout eine (Filter)Eingabe machen, dann drücke ich den Button. Von der Kontaktliste werden die Zeilen jedoch ohne Inhalt angezeigt.
Hier der aktuelle Code und die "main.xml".
Zum Test: Wenn man im Edit-Feld nichts eingibt, müssten alle Kontakte angezeigt werden.

1<?xml version="1.0" encoding="utf-8"?>
2
3<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
4 android:layout_width="fill_parent"
5 android:layout_height="fill_parent"
6 android:padding="6dp"
7 >
8
9 <EditText
10 android:id="@+id/editText1"
11 android:layout_width="wrap_content"
12 android:layout_height="wrap_content"
13 android:layout_alignParentLeft="true"
14 android:layout_alignParentTop="true"
15 android:layout_marginTop="10dp"
16 android:ems="10" >
17
18 <requestFocus />
19 </EditText>
20
21 <ListView
22 android:id="@android:id/list"
23 android:layout_width="match_parent"
24 android:layout_height="wrap_content"
25 android:layout_alignLeft="@+id/editText1"
26 android:layout_below="@+id/editText1"
27 android:layout_marginBottom="30dp" >
28 </ListView>
29
30 <Button
31 android:id="@+id/button1"
32 android:layout_width="wrap_content"
33 android:layout_height="wrap_content"
34 android:layout_above="@android:id/list"
35 android:layout_toRightOf="@+id/editText1"
36 android:text="Button" />
37
38</RelativeLayout>

1import android.app.ListActivity;
2import android.content.Intent;
3import android.database.Cursor;
4import android.database.CursorWrapper;
5import android.net.Uri;
6import android.os.Bundle;
7import android.provider.ContactsContract;
8import android.provider.ContactsContract.Contacts;
9import android.view.KeyEvent;
10import android.view.View;
11import android.view.View.OnClickListener;
12import android.view.View.OnFocusChangeListener;
13import android.widget.AdapterView;
14import android.widget.AdapterView.OnItemClickListener;
15import android.widget.Button;
16import android.widget.EditText;
17import android.widget.ListAdapter;
18import android.widget.ListView;
19import android.widget.SimpleCursorAdapter;
20
21public class MiniContacts extends ListActivity implements OnClickListener {
22
23 private Cursor cursorContacts;
24 String filter="display_name like 'anja%'";
25 EditText ed;
26
27 @Override
28 public void onCreate(Bundle savedInstanceState) {
29 super.onCreate(savedInstanceState);
30 setContentView(R.layout.main);
31 ed = (EditText)findViewById(R.id.editText1);
32 Button button = (Button)findViewById(R.id.button1);
33 button.setOnClickListener(this);
34 }
35 public void onClick(View v) {
36 filter = "display_name like '" + ed.getText().toString()+"%'"; //Achtung: Hochkomma
37 go();
38 }
39
40 private void go() {
41 // Kontaktliste ermitteln
42 cursorContacts = getContentResolver().query(
43 ContactsContract.Contacts.CONTENT_URI, null, filter, null, null);
44 startManagingCursor(cursorContacts);
45 // bildet Datensätze auf Listenelemente ab
46 ListAdapter adapter = new SimpleCursorAdapter(this,
47 android.R.layout.simple_list_item_1, cursorContacts,
48 new String[] { Contacts.DISPLAY_NAME },
49 new int[] { android.R.id.list });
50 setListAdapter(adapter);
51 getListView().setOnItemClickListener(new OnItemClickListener() {
52
53 @Override
54 public void onItemClick(AdapterView<?> parent, View view,
55 int position, long id) {
56 Object o = getListAdapter().getItem(position);
57 if (o instanceof CursorWrapper) {
58 // Welcher Datensatz?
59 CursorWrapper w = (CursorWrapper) o;
60 int columnIndex = w.getColumnIndex(Contacts._ID);
61 long contactId = w.getLong(columnIndex);
62 Uri uri = Uri.withAppendedPath(
63 ContactsContract.Contacts.CONTENT_URI,
64 Long.toString(contactId));
65 // Kontakt anzeigen
66 Intent intent = new Intent(Intent.ACTION_VIEW, uri);
67 startActivity(intent);
68 }
69 }
70 });
71
72 }
73 @Override
74 protected void onDestroy() {
75 super.onDestroy();
76 stopManagingCursor(cursorContacts);
77 }

Gruss Wicki

Antworten