ListView / Adapter - Problem

  • Antworten:2
Jan Kressin
  • Forum-Beiträge: 9

01.07.2011, 13:36:44 via Website

Ich möchte meine eigene List View erstellen und hab dazu natürlich in nem netten Tutorial abgeschaut.
Im groben passiert glaub ich das was passieren soll, nur eins nicht. die ListView wird nicht aktualisiert/angezeigt.
Hier die wichtigen Code-Teile:

listitem.xml:
1<?xml version="1.0" encoding="UTF-8"?>
2<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="fill_parent"
4 android:layout_height="20dp"
5 android:background="#000000">
6 <TextView
7 android:id="@+id/linebox"
8 android:layout_width="fill_parent"
9 android:layout_height="fill_parent">
10 </TextView>
11 <TextView
12 android:id="@+id/directionbox"
13 android:layout_width="fill_parent"
14 android:layout_height="fill_parent">
15 </TextView>
16</LinearLayout>

ListAdapter Klasse:
1package fh.mci.BusStop;
2
3import java.util.ArrayList;
4import android.content.Context;
5import android.view.LayoutInflater;
6import android.view.View;
7import android.view.ViewGroup;
8import android.widget.ArrayAdapter;
9import android.widget.TextView;
10
11public class ListAdapter extends ArrayAdapter<Line>{
12
13 private ArrayList<Line> items;
14
15 public ListAdapter(Context context, int textViewResourceId, ArrayList<Line> items) {
16 super(context, textViewResourceId, items);
17 this.items = items;
18 }
19
20 @Override
21 public View getView(int position, View convertView, ViewGroup parent){
22 View v = convertView;
23 if (v == null) {
24 LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
25 v = vi.inflate(R.layout.listitem, null);
26 }
27 Line l = items.get(position);
28 if(l != null){
29 TextView lb = (TextView) v.findViewById(R.id.linebox);
30 if(lb != null){
31 lb.setText(l.getLine());
32 }
33 TextView db = (TextView) v.findViewById(R.id.directionbox);
34 if(db != null){
35 db.setText(l.getDirection());
36 }
37 }
38 return v;
39 }
40
41}

Aufruf:
1@Override
2 public void onClick(View v) {
3 if(v == byLines){
4 ArrayList<Line> lines = new ArrayList<Line>();
5 lines = mng.getAllLines();
6 this.m_adapter = new ListAdapter(this, R.layout.listitem, lines);
7 setListAdapter(this.m_adapter);
8 Log.i("counter:", "lines: " + lines.size() + " m_adapter: " +m_adapter.getCount());
9 m_adapter.notifyDataSetChanged();
10 }
11 }
Hier hab ich mir zur Kontrolle die Zahl der Objekte in der ArrayList und im Adapter-Objekt ausgeben lassen. Sie stimmen überein.

bylines.xml mit dem ListView Objekt und der id "list"
1<?xml version="1.0" encoding="UTF-8"?>
2<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="fill_parent"
4 android:layout_height="fill_parent"
5 android:gravity="center_horizontal">
6 <LinearLayout
7 android:layout_width="fill_parent"
8 android:layout_height="wrap_content"
9 android:gravity="center_horizontal">
10 <Button
11 android:text=""
12 android:id="@+id/btn_lines"
13 android:layout_width="wrap_content"
14 android:layout_height="wrap_content">
15 </Button>
16 <Button
17 android:text=""
18 android:id="@+id/btn_station"
19 android:layout_width="wrap_content"
20 android:layout_height="wrap_content">
21 </Button>
22 </LinearLayout>
23 <ListView
24 android:id="@+id/android:list"
25 android:layout_width="fill_parent"
26 android:layout_height="fill_parent">
27 </ListView>
28</LinearLayout>

Ich weiß einfach nicht warum der kram nicht auftaucht.
Das LogCat schmeißt keine Exception...
Bitte um Hilfe :)

Antworten
Ansgar M
  • Forum-Beiträge: 1.544

01.07.2011, 19:32:25 via App

Es gibt bei Android bereits eine Klasse mit dem Namen ListAdapter. Könnte es vielleicht daran liegen?
Lg Ansgar

Antworten
Jan Kressin
  • Forum-Beiträge: 9

01.07.2011, 20:08:54 via Website

War leider nicht der Grund hab das Teil jetzt zu LineAdapter umbenannt mittels Refactor. kein erfolg.

Hat noch wer ne Idee? brauch den krams wirklich dringend und finds einfach nicht raus -.-

Also, der einzige Unterschied zwischen der funktionierenden Tutorial Variante und meiner abgewandelten ist im Grunde der Aufrufort.

ich habe ein bestehendes Layout und starte den Listview krams aus einem onClick Event heraus. Daher habe ich diese ListView mit der id "list" nicht in der main.xml sondern in einer anderen xml. Diese ist jedoch in der aufrufenden Activity als ContentView gesetz, wie es sich gehört.

Das Tutorial hat die ListView in der main.xml.

Die row.xml sind bei beiden Varianten identisch.

Warum funktioniert das eine und das andere nicht.

Die App ist eine Activity mit nem TabHost, aus dem TabActivitys gestartet werden. daher die andere layout datei.

— geändert am 01.07.2011, 21:22:27

Antworten