开发者

Call to ListView's setEmpty method not working (nothing is displayed when the list is empty)

I have an Activity which contains a ListView defined in XML (its not subclassing the ListActivity class).

I want to display a message when the ListView is empty, so I tried doing so with the set开发者_运维百科EmptyView method:

listView = (ListView) findViewById(R.id.list);
TextView emptyListText = new TextView(this);
// also tried with the following line uncommented
// emptyListText.setId(android.R.id.empty);
emptyListText.setText(getString(R.string.EmptyList));
listView.setEmptyView(emptyListText);

But it is not working.

Any ideas?

Thanks.


You need to add the following two lines to your code after you call setText():

emptyListText.setVisibility(View.GONE);
((ViewGroup)listView.getParent()).addView(emptyListText);

If the visibility is not set to GONE (hidden), then the TextView will always be rendered visible. The second line places the TextView into the ListView's ViewGroup -- which is automatically handled when using declarative XML Views.


Source: http://www.littlefluffytoys.com/?p=74


Try it this way...

First a simple activity_main.xml layout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <ListView
        android:id="@+id/root_list"
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="1" />

    <ViewStub
        android:id="@+id/root_empty"
        android:layout_width="match_parent"
        android:layout_height="match_parent" 
        android:layout="@layout/empty_view"
        android:visibility="gone" />

</LinearLayout>

and from your MainActivity.java:

public class MainActivity extends Activity {
    ListView mListView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        this.mListView = (ListView) findViewById(R.id.root_list);
        mListView.setEmptyView(findViewById(R.id.root_empty));

        mListView.setAdapter(null);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }    
}

Whatever is placed in R.layout.empty_view will appear whenever the adapter is null or isEmpty returns true.

I used this for the empty_view.xml:

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:background="@color/gray"
        android:gravity="center"
        android:text="@string/list_empty"
        android:textColor="@color/white"
        android:textSize="24sp" />

</LinearLayout>


Hmm, maybe the reason is that the view doesn't have the layout params set. Try to set some layout params to that view, and i think that will work. Something like this:

emptyListText.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

If this doesn't work, try to inflate the view in your ListView's parent (R.id.list), with something like this:

mEmptyListView = (LinearLayout) getLayoutInflater().inflate(
                R.layout.no_items,
                (LinearLayout) findViewById(R.id.list));
mListView.setEmptyView(findViewById(R.id.no_items));

I'm writing it from memory, so let me know if it doesn't work or there is any typo. Basically the idea is that your view should be added to the layout

Ger

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜