I've created a ListFragment
using the following custom layout which adds a TextView
to show a message when the list is empty:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView android:id="@id/android:list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:drawSelectorOnTop="false"/>
<TextView android:id="@id/android:empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/vsl_empty"
android:gravity="center"
android:layout_gravity="center"
android:textColor="#8C8C8C" />
</LinearLayout>
The data is loaded using a custom adapter that derives from CursorAdapter
. When there is data in the database, the list very briefly shows the empty list message until the data is loaded, at which point the list gets populated.
How can I prevent the empty list message from briefly appearing because the list hasn't been populated yet?
Update
I want to emphasise that I'm using a ListFragment
with a layout that contains a ListView
with the special id android:list
, and a TextView
with the special id android:empty
, as explained here.
Setting android:visibility="invisible"
doesn't affect the visibility. I suspect this is because it gets set to visible internally.
just check if (cartlist. size()<0) then yours list is Empty.!
Try this: my_listview. setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View view, int position, long id) { } });
The provided answers were on the right track. I indeed had to set the TextView
to invisible, but the correct location is in onCreateLoader
:
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args)
{
mListView.getEmptyView().setVisibility(ListView.GONE);
return new CursorLoader(getActivity(), CONTENT_URI, null, null, null, null);
}
Also, it's not necessary to make the empty view visible again: this is done automatically by the framework.
How about adding the following to the TextView
android:visibility="invisible"
to the XML layout file, and then making the TextView visible if required.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With