Problema con setEmptyView en una ListActivity
Tengo la disposición siguiente, y el texto vacío de la visión no aparece …
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getListView().setEmptyView(findViewById(R.id.empty)); getListView().setTextFilterEnabled(true); setListAdapter(null);// to have empty list }
El archivo de diseño es:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout android:id="@+id/myScrollLayout" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android"> <FrameLayout android:layout_width="fill_parent" android:layout_height="0dip" android:layout_weight="1"> <ListView android:id="@android:id/list" android:layout_width="fill_parent" android:layout_height="fill_parent" android:drawSelectorOnTop="false" /> <!-- Here is the view to show if the list is emtpy --> <ScrollView android:id="@android:id/empty" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:id="@+id/emptyText" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="@string/contact_empty_help" android:textSize="20sp" android:textColor="?android:attr/textColorSecondary" android:paddingLeft="10dip" android:paddingRight="10dip" android:paddingTop="10dip" android:lineSpacingMultiplier="0.92" /> </ScrollView> </FrameLayout> </LinearLayout>
La cadena se define como:
<string name="contact_empty_help">"You don't have any contacts to display.\n\nTo add contacts, you have to:\n \n<li><font fgcolor="#ffffffff">Go to <b>Contacts</b></font> application to create new contacts\n</li> \n<li><font fgcolor="#ffffffff"><b>Import from VCF file</b></font>, this is available in Contacts menu, the file most be on the root of the SD card\n</li>" </string>
Dado que la lista está vacía, debería mostrarme el texto definido. He seguido el ejemplo de la lista8
https://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/List8.html
https://developer.android.com/resources/samples/ApiDemos/res/layout/list_8.xml
Me faltaba un setContentView(R.layout.listview);
y debido a esto la vista no fue tomada upped por findViewById.
De hecho, no necesita esta línea en su código java
getListView().setEmptyView(findViewById(R.id.empty));
La pareja de Android ID android:id="@android:id/list"
y android:id="@android:id/empty"
son exclusivos !
Saludos cordiales, División-par-cero.
No debería
<ScrollView android:id="@android:id/empty"
ser
<ScrollView android:id="@+id/empty"
? No sé si eso hace alguna diferencia.
¿Está seguro de que setListAdapter(null);
en realidad se inicializa el adaptador, en lugar de simplemente ignorar el comando?