Question

I have this listview in android. now I put a textview set it as emptyview for my listview so it will be shown only when the listview is empty. the problem even the listview has a data. the no data is still showing. but after 7 items or 8. it will be gone. why?

<de.timroes.android.listview.EnhancedListView
        android:id="@+id/data_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#C0C0C0"
        android:minHeight="30dp" >
    </de.timroes.android.listview.EnhancedListView>

    <TextView
        android:id="@android:id/empty"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="No Data"
        android:textSize="20sp" />

</LinearLayout>

the way i set it

lv = (EnhancedListView) view.findViewById(R.id.data_list);
lv.setAdapter(adapter);
lv.setEmptyView(emptyText);

sample output

enter image description here

Was it helpful?

Solution

First of all, your TextView should have an initial visibility of GONE:

android:visibility="gone"

Second, the name

android:id="@android:id/empty"

is reserved for ListActivity/ListFragment, therefore is automatically managed by Android itself

So, I use a similar name... which is different

android:id="@+id/txtEmpty"

Last, but not least, in my code I do:

// Set the empty TextView
txtEmpty = (TextView) v.findViewById(R.id.txtEmpty);
txtEmpty.setText(res.getString(R.string.no_data));
elv.setEmptyView(txtEmpty);

being elv the name of my ExpandableListView (a ListView, in your case)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top