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.
Mein Fragment
fragment_1.xml
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 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;
}
}
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);
}
}
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>
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