Jan Kressin
- Forum-Beiträge: 9
01.07.2011, 13:36:44 via Website
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:
ListAdapter Klasse:
Aufruf:
bylines.xml mit dem ListView Objekt und der id "list"
Ich weiß einfach nicht warum der kram nicht auftaucht.
Das LogCat schmeißt keine Exception...
Bitte um Hilfe
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>
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}
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.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 }
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>
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
Empfohlener redaktioneller Inhalt
Mit Deiner Zustimmung wird hier ein externer Inhalt geladen.
Mit Klick auf den oben stehenden Button erklärst Du Dich damit einverstanden, dass Dir externe Inhalte angezeigt werden dürfen. Dabei können personenbezogene Daten an Drittanbieter übermittelt werden. Mehr Infos dazu findest Du in unserer Datenschutzerklärung.