0

I have a ListFragment that returns a ListView. If there are no items in the list, I'd like to display an alternative TextView instead. Supposedly you can add a TextView android:id="@android:id/empty" element within the XML.

But...

Adding FrameLayout containing both the ListView and the TextView predictably gives me "FrameLayout cannot be cast to ListView".

I can't just add a TextView element either. "The markup in the document following the root element must be wellformed."

And I can't switch to View instead of ListView because, well, it would no longer be a list.

...how am I supposed to do this?

Code:

    @Override
public View onCreateView ( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState ) {

    adapter = new ArrayAdapter<Project> ( getActivity (), android.R.layout.simple_list_item_1 );
    arrayList = ProjectManager.getProjectList ( getActivity () );
    for ( Project p : arrayList ) {
        if ( p.getStatus () == Project.ONGOING ) {
            adapter.add ( p );
        }
    }

    ListView listView = ( ListView ) inflater.inflate ( R.layout.list, container, false );
    listView.setAdapter ( adapter );

    return listView;
}

XML:

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
abc32112
  • 2,457
  • 9
  • 37
  • 53

3 Answers3

1

I have a ListFragment that returns a ListView. If there are no items in the list, I'd like to display an alternative TextView instead. Supposedly you can add a TextView android:id="@android:id/empty" element within the XML.

You can.

Adding FrameLayout containing both the ListView and the TextView predictably gives me "FrameLayout cannot be cast to ListView".

That's because of the bug in your code, where you assume that the results of inflate() is a ListView. Change that to use findViewById() to find the ListView in the inflated layout.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I tried the following, but that gives me "the specified child already has a parent". Code: "ListView listView = ( ListView ) inflater.inflate ( R.layout.list_projects_ongoing, container, false ).findViewById ( R.id.listView );" – abc32112 Nov 11 '13 at 16:46
  • @CaptainHindsight: That line does not give you that error. Something else might, but not that line. Also, you need to return the results of `inflate()` from `onCreateView()`, and in your case, you cannot be, as you are throwing out the `inflate()` results. – CommonsWare Nov 11 '13 at 17:00
0

add a TextView with id empty:

<TextView android:id="@android:id/empty"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="@string/emptyt_message" />

to the same layout

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
0

Please have a look at the following method:

http://developer.android.com/reference/android/widget/AdapterView.html#setEmptyView(android.view.View)

For this to be useful, your fragment should consist of a container (i.e. FrameLayout) containing both the ListView and a TextView with your empty message.

When inflating your layout, specify the view id of the text view. Set this text view's initial visibility to INVISIBLE. The text view's visibility is updated automatically according to the number of elements within your adapter.