In einem Listfragment eine normales Layout laden

  • Antworten:9
ECR
  • Forum-Beiträge: 72

05.10.2012, 11:58:55 via Website

Ich habe eine Listfragment Klasse welche Daten aus einer DB anzeigt. Wenn aber keine Einträge von DB kommen, würde ich einfach gerne ein Layout anzeigen in der einfach ein Satz steht "Keine Daten"

Nur leider geht das irgendwie nicht da diese Methode keine View als Rückgabe erwartet:
Hier mein Code von der Stelle wo es passieren sollte:

1@Override
2 public void onActivityCreated(Bundle savedInstanceState) {
3 super.onActivityCreated(savedInstanceState);
4
5 Cursor cursor = getWeekDates();
6 if(cursor.moveToNext()){
7 String[] columns = new String[] { "start", "zimmerNr", "kuerzel" };
8
9 int[] to = new int[] { R.id.date, R.id.schoolSubjectShort, R.id.roomNumberName };
10
11 DatabaseCursorAdapter dbCursorAdapter = new DatabaseCursorAdapter(getActivity().getApplicationContext(), R.layout.database_items, cursor, columns, to, 1);
12
13 setListAdapter(dbCursorAdapter);
14 }
15 else{
16 Log.d("Layout","Hier sollte ein Layout in der nur ein Satz steht");
17 }
18 }

— geändert am 05.10.2012, 11:59:23

Antworten
ECR
  • Forum-Beiträge: 72

05.10.2012, 13:36:15 via Website

Ich glaubs ja nicht! x-Mal diese Seite durchgelesen und solche Sachen einfach übeflogen!

Ja das ist exakt was ich suche, nur leider haut es nicht ganz hin. Er fügt es als normale Textview unter jeden Eintrag(Leider kann ich keine Bilder uploaden es kommt hier ein Fehler von androidpit) Und Wochen ohne Einträge bleiben weiss...
Das heisst er erkennt es nicht als Empty sondern als normale TextView darum auch pro Eintrag. Denn wie gesagt ohne Eintrag bleibt die Seite einfach leer:




Hier die xml

1<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
2 android:layout_width="fill_parent"
3 android:layout_height="fill_parent"
4 android:stretchColumns="1" >
5
6 <TableRow
7 android:id="@+id/tableRow1"
8 android:layout_width="wrap_content"
9 android:layout_height="wrap_content" >
10 </TableRow>
11
12 <TableRow android:layout_marginTop="10dip" >
13
14 <TextView
15 android:id="@+id/date"
16 android:layout_width="wrap_content"
17 android:layout_height="wrap_content"
18 android:layout_marginLeft="10dip"
19 android:gravity="left"
20 android:padding="3dip"
21 android:textColor="#000000"
22 android:textSize="18dip" >
23 </TextView>
24
25 <TextView
26 android:id="@+id/roomNumberName"
27 android:layout_width="wrap_content"
28 android:layout_height="wrap_content"
29 android:gravity="center"
30 android:padding="3dip"
31 android:textColor="#000000"
32 android:textSize="18dip" >
33 </TextView>
34
35 <TextView
36 android:id="@+id/schoolSubjectShort"
37 android:layout_width="wrap_content"
38 android:layout_height="wrap_content"
39 android:layout_marginRight="10dip"
40 android:gravity="right"
41 android:padding="3dip"
42 android:textColor="#000000"
43 android:textSize="18dip" >
44 </TextView>
45
46 </TableRow>
47 <TextView android:id="@id/android:empty"
48 android:layout_width="match_parent"
49 android:layout_height="match_parent"
50 android:text="Keine Einträge"/>
51</TableLayout>

Antworten
ECR
  • Forum-Beiträge: 72

06.10.2012, 09:29:28 via Website

Das ist mein Layout welches die ListView erstellt?! :-) Vielleicht gibt es auch schwierigkeiten da es sich um ein ListFragment handelt?

— geändert am 06.10.2012, 10:07:24

Antworten
Gelöschter Account
  • Forum-Beiträge: 694

06.10.2012, 10:37:13 via Website

Wenn Du das so haben willst dann musst Du wie in der Doku angegeben ein eigenes CustomLayout im onCreateView hinterlegen. Hast Du das? Sieh bitte in die Doku.

ListFragment has a default layout that consists of a single list view. However, if you desire, you can customize the fragment layout by returning your own view hierarchy from onCreateView(LayoutInflater, ViewGroup, Bundle). To do this, your view hierarchy must contain a ListView object with the id "@android:id/list" (or list if it's in code)

— geändert am 06.10.2012, 10:45:25

Antworten
ECR
  • Forum-Beiträge: 72

06.10.2012, 12:33:21 via Website

Mhm das habe ich eigentlich so implentiert. Aber es zeigt mir nun nur immer das Layout an welches im onCreateView ist an. Egal ob ich Daten habe oder nicht.

Das weekFragement ist das normale Layout:
1<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
2 android:layout_width="match_parent"
3 android:layout_height="match_parent"
4 android:orientation="vertical" >
5
6 <TableLayout
7 android:layout_width="fill_parent"
8 android:layout_height="fill_parent"
9 android:stretchColumns="*" >
10
11 <TableRow
12 android:id="@+id/columnName"
13 android:layout_width="wrap_content"
14 android:layout_height="wrap_content" >
15
16 <TextView
17 android:id="@+id/Datum"
18 android:layout_width="wrap_content"
19 android:layout_height="wrap_content"
20 android:text="@string/dateName"
21 android:textSize="20dip" />
22
23 <TextView
24 android:id="@+id/subjectName"
25 android:layout_width="wrap_content"
26 android:layout_height="wrap_content"
27 android:text="@string/subjectName"
28 android:textSize="20dip" />
29
30 <TextView
31 android:id="@+id/roomNumberName"
32 android:layout_width="wrap_content"
33 android:layout_height="wrap_content"
34 android:gravity="right"
35 android:text="@string/roomNumberName"
36 android:textSize="20dip" />
37 </TableRow>
38 </TableLayout>
39
40 <ListView android:id="@id/android:list"
41 android:layout_width="match_parent"
42 android:layout_height="match_parent"
43 android:background="#00FF00"
44 android:layout_weight="1"
45 android:drawSelectorOnTop="false"/>
46
47 <TextView android:id="@id/android:empty"
48 android:layout_width="match_parent"
49 android:layout_height="match_parent"
50 android:background="#FF0000"
51 android:text="No data"/>
52
53</LinearLayout>

Und hier ist die Listfragment mit den 2 Aufrufen. Stimmen die denn?

1@Override
2 public View onCreateView(LayoutInflater inflater, ViewGroup container,
3 Bundle savedInstanceState) {
4 // TODO Auto-generated method stub
5 LinearLayout rl = (LinearLayout) inflater.inflate(R.layout.week_fragment, container, false);
6 return rl;
7
8
9
10
11 }
12
13
14
15 @Override
16 public void onActivityCreated(Bundle savedInstanceState) {
17 super.onActivityCreated(savedInstanceState);
18
19
20
21 Cursor cursor = getWeekDates();
22
23 String[] columns = new String[] { "start", "zimmerNr", "kuerzel" };
24
25 int[] to = new int[] { R.id.date, R.id.schoolSubjectShort, R.id.roomNumberName };
26
27 DatabaseCursorAdapter dbCursorAdapter = new DatabaseCursorAdapter(getActivity().getApplicationContext(), R.layout.database_items, cursor, columns, to, 1);
28
29 setListAdapter(dbCursorAdapter);
30 }

Antworten
ECR
  • Forum-Beiträge: 72

08.10.2012, 07:31:54 via App

Ähm was o_O Du weisst nicht mehr weiter? Na dann ist es ja kein Wunder das ich nicht auf die Lösung kommen kann :-) Das heisst du denkst das eigentlich alles stimmt vo den Aufrufen und so?

Antworten
Gelöschter Account
  • Forum-Beiträge: 694

08.10.2012, 08:38:24 via App

Lass mal spaßeshalber den Background weg.

Ich würde einfach mal die beiden Methoden, das Erzeugen des Cursors sowie den Adapter im Debugger verfolgen. Setz ein paar Breakpoints.

Den empty habe ich bisher nur in Activities verwenden - nicht in Fragmenten. An Hand der Doku ist das aber exakt beschrieben wie es geht. Teste das mal im Debugger.

— geändert am 08.10.2012, 09:25:14

Antworten