Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ListView shows empty message briefly before data is loaded

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.

like image 815
Jon Avatar asked Jan 02 '13 15:01

Jon


People also ask

How check ListView is empty?

just check if (cartlist. size()<0) then yours list is Empty.!

How to get data from ListView by clicking item on ListView?

Try this: my_listview. setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View view, int position, long id) { } });


2 Answers

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.

like image 138
Jon Avatar answered Oct 01 '22 15:10

Jon


How about adding the following to the TextView

android:visibility="invisible"

to the XML layout file, and then making the TextView visible if required.

like image 45
BENBUN Coder Avatar answered Oct 01 '22 14:10

BENBUN Coder