Zweizeiliges Listview zeigt nichts an

  • 2 Antworten
  • Letztes Antwortdatum
1

123thomas

Fortgeschrittenes Mitglied
44
Hallo Leute,

ich habe ein komisches Problem. Mein Listview zeigt nichts an. Das komische daran ist, dass ich die gleiche Konfiguration in einem anderen Projekt habe und da funktioniert es.

Hier der Code:
MainActivity onCreate
Code:
AnzeigeVerbindung = (ImageButton) findViewById(R.id.AnzeigeVerbindung);
        listview = (ListView) findViewById(R.id.liste);

        pDialog = new ProgressDialog(MainActivity.this);
        pDialog.setMessage("Starte Einkaufszettel");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();

        //Gebe den Context
        context = getApplicationContext();

        //Liste Einträge erstellen
        verwalEintraege = new VerwalEintraege();
        //Alles auf Null setzen
        verwalEintraege.löscheAlles();
        //Liste aus Einträge erstellen
        myBaseAdapter = new MyBaseAdapter(verwalEintraege.getListEintraege(), getContext());
        //Einträge in Listview setzen
        listview.setAdapter(myBaseAdapter);

        listview.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {

                Variablen.aktuellerEintrag = verwalEintraege.getEintrag(position);
                Variablen.positionaktuellerEintrag = position;

                //Erstelle eine instanz des PopupMenus
                PopupMenu popup = new PopupMenu(MainActivity.this, view);
                //hole das XML und erstelle ein Menü
                popup.getMenuInflater().inflate(R.menu.popup_item, popup.getMenu());

                //erstelle ein OnClickListener auf den Optionen
                popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    public boolean onMenuItemClick(MenuItem item) {
                        Toast.makeText(MainActivity.this, "Ups, da ist etwas schief gelaufen!", Toast.LENGTH_SHORT).show();

                        return true;
                    }
                });
                Popupmenugeschlossen = false;
                popup.show();
            }
        });

        listview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {

                if (verwalEintraege.getListEintraege()[i].getStrike() == false) {
                    Log.d("Langer Click", "Eintrag: " + (i) + " wird durchgestrichen");
                    //Item durchstreichen
                    TextView item1 = (TextView) getViewByPosition(i, listview).findViewById(R.id.line_a);
                    item1.setPaintFlags(item1.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
                    TextView item2 = (TextView) getViewByPosition(i, listview).findViewById(R.id.line_b);
                    item2.setPaintFlags(item2.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
                    //durchstreichen eintragen
                    verwalEintraege.getEintrag(i).toggleStrike();
                } else {
                    Log.d("Langer Click", "Eintrag: " + (i) + " wird nicht mehr durchgestrichen");
                    //Item nicht mehr durchstreichen
                    TextView item3 = (TextView) getViewByPosition(i, listview).findViewById(R.id.line_a);
                    item3.setPaintFlags(item3.getPaintFlags() & (~Paint.STRIKE_THRU_TEXT_FLAG));
                    TextView item4 = (TextView) getViewByPosition(i, listview).findViewById(R.id.line_b);
                    item4.setPaintFlags(item4.getPaintFlags() & (~Paint.STRIKE_THRU_TEXT_FLAG));
                    //durchstreichen austragen
                    verwalEintraege.getEintrag(i).toggleStrike();
                }

                return true;
            }
        });

MyBaseAdapter:

Code:
package com.example.einkaufszettel;

import android.app.AlertDialog;
import android.net.ConnectivityManager;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.content.Context;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
* Created by Thomas on 28.10.2015.
*/
class MyBaseAdapter extends BaseAdapter {

    //private ArrayList<Eintrag[]> einträge;
    private Eintrag[] ListEintraege = new Eintrag[100];
    private boolean ZeigeToast = true;
    private Context context;


    public MyBaseAdapter(Eintrag[] ListEintraege, Context context) {
        ////einträge nur alibi damit Baseadapter erstellt werden kann Daten werden aus aus den Gegenstand unf Anzahlarray genommen
        //this.einträge = eint;
        this.ListEintraege = ListEintraege;
        this.context = context;
    }

    public void setListEintraege(Eintrag[] ListeEinträge)
    {
        this.ListEintraege = ListeEinträge;
    }

    @Override
    public int getCount() {
        return 0;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = (View) inflater.inflate(R.layout.listview_two_lines, null);
        } else {
            convertView = (View) convertView;
        }
        TextView line1 = (TextView) convertView.findViewById(R.id.line_a);
        TextView line2 = (TextView) convertView.findViewById(R.id.line_b);

        Log.d("Listview:line1", ListEintraege[position].getGegenstand() + " line2: " + ListEintraege[position].getAnzahl()+ " Eintrag: " + String.valueOf(position));
        line1.setText(ListEintraege[position].getGegenstand());
        line2.setText(ListEintraege[position].getAnzahl());

        return convertView;
    }

    public void showToast(final String toast) //Toast in einem Thread anzeigen
    {
        if (ZeigeToast) {
            //Toast.makeText(MainActivity.this, toast, Toast.LENGTH_SHORT).show();

            //kleiner Hinweis das die Zahl zu lang
            Context context = MainActivity.getContext();
            CharSequence text = toast;
            int duration = Toast.LENGTH_SHORT; //Nur kurz anzeigen

            Toast.makeText(context, text, duration).show();
        }

    }

    public void showAlert(String Message, String Buttontxt) {
        final String Buttontxt1 = Buttontxt;
        final String Message1 = Message;
        new AlertDialog.Builder(MainActivity.getContext()).setNeutralButton(Buttontxt1, null).setMessage(Message1).show();
    }

}

Main XMl
Code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingLeft="5dp"
    android:paddingRight="5dp"
    android:background="@color/grau"
    android:orientation="vertical"
    tools:context=".MainActivity" >
  
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.5"
        android:background="@color/grau"
        android:gravity="center"
        android:paddingTop="5dp"
        android:layout_gravity="top"
        android:orientation="horizontal"
        tools:context=".MainActivity" >
      
        <TextView
             android:id="@+id/LabelVerbindung"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
            android:layout_weight="1"
             android:background="@color/grau"
             android:clickable="false"
             android:gravity="center"
             android:inputType="none"
             android:text="@string/Verbindung"
             android:textColor="@color/white"
             android:textSize="15sp"
             android:textStyle="bold" />
      
        <ImageButton
        android:id="@+id/AnzeigeVerbindung"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="5"
        android:onClick="onClickButtonVerbindung"
        android:background="@drawable/anzeigeverbindungrot"
        android:textColor="@color/white"
        android:textSize="15sp" />    
                 
        </LinearLayout>
  
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="5"
        android:background="@color/grau"
        android:gravity="center"
        android:paddingTop="5dp"
        android:layout_gravity="top"
        android:orientation="horizontal"
        tools:context=".MainActivity"
        android:weightSum="1">
      
    <ListView
        android:id="@+id/liste"
        android:layout_width="wrap_content"
        android:layout_height="match_parent" />
      
    </LinearLayout>
  
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="@color/grau"
        android:gravity="center"
        android:layout_gravity="top"
        android:orientation="horizontal"
        tools:context=".MainActivity" >
  
    <Button
        android:id="@+id/ButtonNeuerEintrag"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@drawable/button_blue"
        android:onClick="onClickButtonNeuerEintrag"
        android:text="@string/Button_NeuerEintrag"
        android:textColor="@color/white"
        android:textSize="15sp" />
  
    </LinearLayout>
</LinearLayout>

listview_two_lines.xml
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/line_a"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textColor="#FFFFFF"
        android:textStyle="bold"
        android:textSize="17dp" />

    <TextView
        android:id="@+id/line_b"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textColor="#FFFFFF"
        android:textSize="15dp"
        android:textStyle="italic" />

</LinearLayout>

Kann mir da jemand weiterhelfen.
Auch ein notiyDataSetChange bringt keine Anzeige. Dann habe ich geloggt(
Code:
Log.d("Listview:line1", ListEintraege[position].getGegenstand() + " line2: " + ListEintraege[position].getAnzahl()+ " Eintrag: " + String.valueOf(position));)
in der MyBaseAdapter Klasse ob getView aufgerufen wird und das wird aufgerufen mit den richtigen Inhalten.

Vielen Dank

Gruß Thomas
 
In der Methode getCount() von deinem Adapter gibst du 0 zurück. Das heißt der Adapter geht davon aus, dass du keine Daten zum anzeigen hast und zeigt deswegen auch keine an.

Code:
@Override
public int getCount() {
    return ListEintraege.lenght;
}

Und noch als kleiner Hinweis, Variablennamen beginnen in Java normalerweise mit einem kleinen Buchstaben :)
 
  • Danke
Reaktionen: 123thomas
Vielen Dank das habe ich beim Copy Paste einfach überschreiben ohne Nachzudenken.

Ja das werde ich ändern.
 
Zurück
Oben Unten