Custom ListView zeigt keine Daten

  • 1 Antworten
  • Letztes Antwortdatum
jan1805

jan1805

Neues Mitglied
1
Hallo,
ich versuche in einem ListView verschiedene Daten (Bücher) anzuzeigen.
Habe bereits mehrere Tutorials durchgesehen und mich schlussendlich an dem von Android ListView - Tutorial entlang gehangelt.
Leider bekomme ich überhaupt keine Daten angezeigt. Alle Fehlermeldungen habe ich beseitigt und im Debugger komme ich auch nicht mehr tiefer.
Ich würde mich freuen, wenn mal jemand über meinen Code drüber sehen kann und (hoffentlich) den/die Fehler findet.

Das komplette Projekt habe ich unter https://www.dropbox.com/s/6u9ul08gl4m8lob/ListViewExample.zip online stehen.
Ansonsten kommt der Quellcode im Anschluss.

Das ganze soll später unter API8 laufen und die ListView ist später in einem Fragment untergebracht. Da es dort aber nicht lief, habe ich es erstmal in eine einzelne App ausgegliedert. Zum Testen wollte ich die Bilder erstmal weglassen, kann das Probleme machen (siehe Adapter)?

Vielen Dank im Voraus,
Jan

Layout für ListViewAdapter:
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:paddingTop="10dp" 
    android:id="@+id/listview_buch">

    <!-- ggf höhe * breite :136dip * 96dip -->
    <ImageView
        android:id="@+id/listview_icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:paddingLeft="4dp" >

        <TextView
            android:id="@+id/listview_title"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@+id/listview_titel"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <TextView
            android:id="@+id/listview_subtitle"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@+id/listview_subtitel"
            android:textAppearance="?android:attr/textAppearanceMedium" />

    </LinearLayout>

</LinearLayout>
Layout meiner App:
Code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/listview_buch" >
    </ListView>

</RelativeLayout>
Meine App Klasse:
Code:
public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        ListView bibliothekLV = (ListView)findViewById(R.id.listView1);
        
        List<Book> books = new ArrayList<Book>(0);
        Book a = new Book();
        a.setAutor("a");
        a.setTitel("A");
        
        Book b = new Book();
        b.setAutor("b");
        b.setTitel("B");
        
        Book c = new Book();
        c.setAutor("c");
        c.setTitel("C");
        
        Book d = new Book();
        d.setAutor("d");
        d.setTitel("D");
        
        books.add(a);
        books.add(b);
        books.add(c);
        books.add(d);

        BookAdapter adapter = new BookAdapter(this.getApplicationContext(), R.layout.listview_buch, books);
        bibliothekLV.setAdapter(adapter);
        
    }
Mein Adapter:
Code:
public class BookAdapter extends ArrayAdapter<Book>{

    Context context;
    int layoutResourceId;   
    //Book data[] = null;
    List<Book> data=null;
    
   
    public BookAdapter(Context context, int layoutResourceId, List<Book> data) {
        super(context, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = data;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        BookHolder holder = null;
       
        if(row == null)
        {
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);
           
            holder = new BookHolder();
            holder.icon = (ImageView)row.findViewById(R.id.listview_icon);
            holder.title = (TextView)row.findViewById(R.id.listview_title);
            holder.subtitle = (TextView)row.findViewById(R.id.listview_subtitle);
            
            row.setTag(holder);
        }
        else
        {
            holder = (BookHolder)row.getTag();
        }
       
        Book book = data.get(position);
        holder.title.setText(book.getAutor() );
        holder.subtitle.setText(book.getTitel());
        if(!(book.getBild()==null))
            holder.icon.setImageBitmap(book.getBild());
        else
             holder.icon.setImageResource(R.drawable.ic_launcher);
       
        return row;
    }
   
    static class BookHolder
    {
        public TextView title;
        public TextView subtitle;
        public ImageView icon;
      }
}
Die Buch-Klasse:
Code:
public class Book {
    
    private long id;
    private String titel;
    private String autor;
    private int isbn;
    private String comment;
    private Bitmap bild;
    private int status;
    private int ressource;

    public long getId() {
        return id;
    }
    public void setId(long id) {
        this.id = id;
    }

    public String getTitel() {
        return titel;
    }
    public void setTitel(String titel) {
        this.titel = titel;
    }
    
    public String getAutor() {
        return autor;
    }
    public void setAutor(String autor) {
        this.autor = autor;
    }
    
    public int getISBN() {
        return isbn;
    }
    public void setISBN(int isbn) {
        this.isbn = isbn;
    }
    
    public String getBeschreibung() {
        return comment;
    }
    public void setBeschreibung(String Beschreibung) {
        this.comment = Beschreibung;
    }
    
    public Bitmap getBild() {
        return bild;
    }
    public void setBild(Bitmap bild) {
        this.bild = bild;
    }
    
    public Integer getStatus() {
        return status;
    }
    public void setStatus(int status) {
        this.status = status;
    }
    
    public Integer getRessource() {
        return ressource;
    }
    public void setRessource(int ressource) {
        this.ressource = ressource;
    }
}


Der ursprüngliche Beitrag von 17:47 Uhr wurde um 18:00 Uhr ergänzt:

Kommando Zurück....
Kaum hat man es abgesendet und sich noch einmal alles in Ruhe durchgesehen, fällt es einem wie Schuppen von den Augen!!!:cursing:
Habe im BookAdapter die Zeile
Code:
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
durch
Code:
LayoutInflater inflater = LayoutInflater.from(context);
ersetzt.

Vielleicht fällt ja trotzdem noch jemandem was auf...:cursing:
 
  • Danke
Reaktionen: DieGoldeneMitte
Schön wenn jemand postet, dass er seine Lösung gefunden hat! :thumbup:
 
Zurück
Oben Unten