ArrayList<Class> wird nicht auf Null gesetzt

T

theAydinator

Neues Mitglied
0
Guten Abend Forum,

ich versuche in einem Fragment mehrere ListViews mit Inhalt per SqLite auszufüllen. Dazu verwende ich einen CustomAdapter.

Code:
public class CustomAdapter extends ArrayAdapter<Kapitel>{
   
    // Declare Variables
    Context context;
    LayoutInflater inflater;
    private ArrayList<Kapitel> hisnulmuslim_kapitel_this = null;
    private ArrayList<Kapitel> arraylist = null;
   
    public CustomAdapter(Context context, int textViewResourceId, ArrayList<Kapitel> hisnulmuslim_kapitel) {
        super(context, textViewResourceId, hisnulmuslim_kapitel);

        hisnulmuslim_kapitel_this = hisnulmuslim_kapitel;
        this.context = context;

        inflater = LayoutInflater.from(context);
 
        this.arraylist = new ArrayList<Kapitel>();
        this.arraylist.addAll(hisnulmuslim_kapitel);
    }
 
    public static class ViewHolder{
        TextView titel;
    }
 
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        ViewHolder holder;
        if (v == null) {
            //LayoutInflater vi = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = inflater.inflate(R.layout.list_item, null);           

            // text view label
            TextView tv_titel = (TextView) v.findViewById(R.id.list_item_titel);
           
            holder = new ViewHolder();
            holder.titel = tv_titel;
            v.setTag(holder);
        }
        else
            holder=(ViewHolder)v.getTag();
 
        final Kapitel custom = hisnulmuslim_kapitel_this.get(position);
       
        if (custom != null) {
            holder.titel.setText(custom.getTitel());
        }
       
        return v;
    }
}
Mein Fragment
Code:
public class Fragment1 extends Fragment {

    DatabaseAdapter dbHelper;

    ListView listView;
    private CustomAdapter adapter;
    ArrayList<Kapitel> hisnulmuslim_kapitel;
   
    ListView listView2;
    private CustomAdapter adapter2;
    ArrayList<Kapitel> hisnulmuslim_kapitel2;
   
    ListView listView3;
    private CustomAdapter adapter3;
    ArrayList<Kapitel> hisnulmuslim_kapitel3;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_1, container, false);

        dbHelper = new DatabaseAdapter(getActivity());
        dbHelper.open();

            // ListView 1
            hisnulmuslim_kapitel = dbHelper.getBittgebeteNachUnterkategorie(1);
           
            listView = (ListView) rootView.findViewById(R.id.listview_1);
   
            adapter = new CustomAdapter(getActivity(),
                    R.id.listview_1,
                    hisnulmuslim_kapitel);

            listView.setAdapter(adapter);
            adapter.notifyDataSetChanged();
            setListViewHeightBasedOnChildren(listView);


            // ListView 2
            hisnulmuslim_kapitel2 = dbHelper.getBittgebeteNachUnterkategorie(2);
           
            listView2 = (ListView) rootView.findViewById(R.id.listview_2);
   
            adapter2 = new CustomAdapter(getActivity(),
                    R.id.listview_2,
                    hisnulmuslim_kapitel2);

            listView2.setAdapter(adapter2);
            adapter2.notifyDataSetChanged();
            setListViewHeightBasedOnChildren(listView2);


            // ListView 2
            hisnulmuslim_kapitel3 = dbHelper.getBittgebeteNachUnterkategorie(3);
           
            listView3 = (ListView) rootView.findViewById(R.id.listview_3);
   
            adapter3 = new CustomAdapter(getActivity(),
                    R.id.listview_3,
                    hisnulmuslim_kapitel3);

            listView3.setAdapter(adapter3);
            adapter3.notifyDataSetChanged();
            setListViewHeightBasedOnChildren(listView3);
       
        dbHelper.close();

        return rootView;
    }
   
    public static void setListViewHeightBasedOnChildren(ListView listView) {
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            // pre-condition
            return;
        }

        int totalHeight = 0;
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        listView.setLayoutParams(params);
    }
}
fragment_1.xml
Code:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fillViewport="true" >

    <LinearLayout
        android:id="@+id/ll_fragment"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:background="@color/farbe_1" >
       
        <LinearLayout
            android:id="@+id/ll_fragment_1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
           
            <TextView
                android:id="@+id/headline_1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:padding="10dp"
                android:text="@string/unterkat_1"
                android:textColor="@color/white"
                android:textStyle="bold"
                android:textSize="15sp"
                android:fontFamily="sans-serif-medium" />
           
            <ListView
                android:id="@+id/listview_1"
                android:background="@color/list_bg"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:layout_marginTop="5dp" />
               
        </LinearLayout>
       
        <LinearLayout
            android:id="@+id/ll_fragment_2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
           
            <TextView
                android:id="@+id/headline_2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:padding="10dp"
                android:text="@string/unterkat_2"
                android:textColor="@color/white"
                android:textStyle="bold"
                android:textSize="15sp"
                android:fontFamily="sans-serif-medium" />   
           
            <ListView
                android:id="@+id/listview_2"
                android:background="@color/list_bg"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:layout_marginTop="5dp" />
               
        </LinearLayout>
       
        <LinearLayout
            android:id="@+id/ll_fragment_3"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
       
            <TextView
                android:id="@+id/headline_3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:padding="10dp"
                android:text="@string/unterkat_3"
                android:textColor="@color/white"
                android:textStyle="bold"
                android:textSize="15sp"
                android:fontFamily="sans-serif-medium" />
           
            <ListView
                android:id="@+id/listview_3"
                android:background="@color/list_bg"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:layout_marginTop="5dp" />
               
        </LinearLayout>

    </LinearLayout>
</ScrollView>
Ergebnis sieht vereinfacht folgendermaßen aus:

Unterkategorietitel1
Listeneintrag von der Unterkateogie 1

Unterkategorietitel2
Listeneintrag von der Unterkateogie 1
Listeneintrag von der Unterkateogie 2
Listeneintrag von der Unterkateogie 2
Listeneintrag von der Unterkateogie 2

Unterkategorietitel3
Listeneintrag von der Unterkateogie 1
Listeneintrag von der Unterkateogie 2
Listeneintrag von der Unterkateogie 2
Listeneintrag von der Unterkateogie 2

Listeneintrag von der Unterkateogie 3
Listeneintrag von der Unterkateogie 3
Listeneintrag von der Unterkateogie 3
Listeneintrag von der Unterkateogie 3
Listeneintrag von der Unterkateogie 3


Ich denke, das Problem liegt daran, dass ArrayList<Kapitel> nicht auf Null gesetzt wird.
Ist dieser Gedanke richtig?
Wenn ja, wie ?

Ich habe mit der Methode clear() versucht, leider vergeblich.

Ich würde mich über Tipps freuen.
Einen schönen Abend wünsche ich Euch noch.

Grüße
Aydin
 
Ich würde eher auf eine fehlerhafte Datenbankabfrage tippen.
 
markus.tullius schrieb:
Ich würde eher auf eine fehlerhafte Datenbankabfrage tippen.

Danke, für deine Antwort.

Wenn ich die Listen einzeln abrufe, dann gibt es keine Probleme, aber dann habe ich eben eine Liste in der Activity. An der Abfrage liegt es meiner Meinung nach nicht, anbei der Code.

Code:
public ArrayList<Kapitel> getBittgebeteNachUnterkategorie(int unterkategorie_id) {
        
        cursor = db.query(    DBHelper.TABLE_NAME_KAPITELN,
                            new String[] { DBHelper.ID_COLUMN , DBHelper.TITEL_COLUMN },
                            DBHelper.UNTERKATEGORIE_COLUMN + "=?",
                            new String[] { String.valueOf(unterkategorie_id) },
                            null, null, null, null);
        
        cursor.moveToFirst();
        do {
            kapitel = new Kapitel();
            
            int id = cursor.getInt(0);
            String titel = cursor.getString(1);

            kapitel.setId(id);
            kapitel.setTitel(titel);
            
            hisnulmuslim_kapitel.add(kapitel);
        } while (cursor.moveToNext());
        
        return hisnulmuslim_kapitel;
    }
 
Der Fehler liegt in der Zeile:

hisnulmuslim_kapitel.add(kapitel);


Die Liste hisnulmuslim_kapitel wird bei jeder Datenbankabfrage um ein Kapitel erweitert.
1. Abfrage 1. Kapitel
2. Abfrage 1. Kapitel + 2. Kapitel
3. Abfrage 1. Kapitel + 2. Kapitel + 3. Kapitel

Lösung:

Code:
do {
            kapitel = new Kapitel();
             
[B]             [I]hisnulmuslim_kapitel = new ArrayList<Kapitel>();[/I][/B]

            int id = cursor.getInt(0);
            String titel = cursor.getString(1);
            kapitel.setId(id);
            kapitel.setTitel(titel);

            hisnulmuslim_kapitel.add(kapitel);
} while (cursor.moveToNext());
 
markus.tullius schrieb:
Der Fehler liegt in der Zeile:

hisnulmuslim_kapitel.add(kapitel);


Die Liste hisnulmuslim_kapitel wird bei jeder Datenbankabfrage um ein Kapitel erweitert.
1. Abfrage 1. Kapitel
2. Abfrage 1. Kapitel + 2. Kapitel
3. Abfrage 1. Kapitel + 2. Kapitel + 3. Kapitel

Lösung:

Code:
do {
            kapitel = new Kapitel();
             
[B]             [I]hisnulmuslim_kapitel = new ArrayList<Kapitel>();[/I][/B]

            int id = cursor.getInt(0);
            String titel = cursor.getString(1);
            kapitel.setId(id);
            kapitel.setTitel(titel);

            hisnulmuslim_kapitel.add(kapitel);
} while (cursor.moveToNext());

Danke, für deinen Tipp, es geht auf jeden Fall in die richtige Richtung.
Bloß, bekomme ich nur den letzten Datensatz der Abfrage.

Ergebnis
Liste 1
Letzte Datensatz der Abfrage
Liste 2
Letzte Datensatz der Abfrage
Liste 3
Letzte Datensatz der Abfrage

Aktueller Code
Code:
public ArrayList<Kapitel> getBittgebeteNachUnterkategorie(int unterkategorie_id) {
        
        cursor = db.query(    DBHelper.TABLE_NAME_KAPITELN,
                            new String[] { DBHelper.ID_COLUMN , DBHelper.TITEL_COLUMN },
                            DBHelper.UNTERKATEGORIE_COLUMN + "=?",
                            new String[] { String.valueOf(unterkategorie_id) },
                            null, null, null, null);


        while (cursor.moveToNext()) {
            kapitel = new Kapitel();

            hisnulmuslim_kapitel = new ArrayList<Kapitel>();

            int id = cursor.getInt(0);
            String titel = cursor.getString(1);

            kapitel.setId(id);
            kapitel.setTitel(titel);
            
            hisnulmuslim_kapitel.add(kapitel);
        }
        
        return hisnulmuslim_kapitel;
}


Der ursprüngliche Beitrag von 10:18 Uhr wurde um 10:27 Uhr ergänzt:

Code:
kapitel = new Kapitel();

        hisnulmuslim_kapitel = new ArrayList<Kapitel>();
        
        while (cursor.moveToNext()) {
            int id = cursor.getInt(0);
            String titel = cursor.getString(1);

            kapitel.setId(id);
            kapitel.setTitel(titel);
            
            hisnulmuslim_kapitel.add(kapitel);
}

Wenn ich so schreibe, erhalte ich bei einer Abfrage den letzten Datensatz mehrfach aufgelistet.
 
Hat keiner eine passende Idee?
 

Ähnliche Themen

M
  • MikelKatzengreis
Antworten
10
Aufrufe
226
swa00
swa00
M
  • MikelKatzengreis
Antworten
5
Aufrufe
131
swa00
swa00
B
Antworten
4
Aufrufe
494
bb321
B
Zurück
Oben Unten