KDEx
KDEx

Reputation: 3657

Force EmptyView to display?

I have a ListView that I am creating. The content is set from a separate adapter class. When the adapter returns zero items I want the empty view to display from a separate xml. This is happening inside a SherlockFragment. However for some reason I cannot get the emptyView to display.

Help Please?

Code:

    RelativeLayout fragView = new RelativeLayout(getActivity());
    ListView listView = new ListView(getActivity());
        listView.setEmptyView(getActivity().findViewById(R.id.emptyView));//This is having no effect
        scoreViewAdapter = new ScoreViewAdapter(getActivity(),
                databaseHelper.getAllTimeRecords());
        listView.setAdapter(scoreViewAdapter);
        if (scoreViewAdapter.getCount() < 1)
                  listView.//can I set something here to force or change my view? From the methods available to ListView I don't see any potential solutions..
        fragView.addView(listView);

Here is the xml....pretty standard.:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/emptyView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/emptyText"
    android:src="@drawable/ic_android_logo" />

<TextView
    android:id="@+id/emptyText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:gravity="center_vertical"
    android:text="No Items Found"
    android:textAppearance="?android:attr/textAppearanceLarge" />

Thanks to Faizan's feedback to lead me here. Here is the solution:

    View empty_view = inflater.inflate(R.layout.empty_view, null);
    ...
    if (scoreViewAdapter.getCount() < 1)
         fragView.addView(empty_view);
    fragView.addView(listView);

Upvotes: 0

Views: 153

Answers (2)

Faizan
Faizan

Reputation: 3512

You have to inflate that xml using LayoutInflater and then call

listView.setEmptyView(inflated_view);

You can get get the inflater from the onCreateView overrided method anc do

View inflated_view = inflater.inflate(R.layout.my_empty_layout,null);

I hope it will resolve the problem.

Upvotes: 2

Tomer Mor
Tomer Mor

Reputation: 8028

I believe you extends FragmentList and not ListActivity so you actually can use setEmptyView

Upvotes: 0

Related Questions