2014-10-03 68 views
-1

如何将空视图添加到listViewReadStories将emptyView设置为ListView

<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

请将此项添加到您的xm L以下,你的ListView 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); 
相关问题