将emptyView设置为ListView

时间:2014-10-03 13:56:09

标签: android android-listview

如何将空视图添加到listViewReadStories

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/read_stories_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ActivityReadStories" >

<FrameLayout
    android:id="@+id/frame_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ListView
        android:id="@+id/listViewReadStories"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:divider="@null"
        android:listSelector="@drawable/newslist_selector" >
    </ListView>
</FrameLayout>

<ListView
    android:id="@+id/list_slidermenu"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="#3d5963"
    android:choiceMode="singleChoice"
    android:divider="#193540"
    android:dividerHeight="1dp"
    android:listSelector="@drawable/nav_selector" />
</android.support.v4.widget.DrawerLayout>

我正在尝试将此布局设置为emptyView:

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

<TextView
 android:id="@+id/empty1"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:gravity="center_horizontal"
 android:text="Nothing in the List!"
/>

这是我的java代码:

TextView empty = (TextView)findViewById(R.id.empty1);
FrameLayout rootLayout = (FrameLayout)findViewById(R.id.frame_container);
rootLayout.addView(empty, 0);
listview.setEmptyView(empty);

但我得到致命的异常

1 个答案:

答案 0 :(得分:1)

请在listview xml

下方的xml中添加
<TextView android:id="@android:id/empty"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="No Results" />

并在您的代码中

TextView empty = (TextView)findViewById(android.R.id.empty);
listview.setEmptyView(empty);