我正在制作ListView
。内容是从单独的适配器类设置的。当适配器返回零项时,我希望从单独的xml显示空视图。这发生在SherlockFragment
内。但由于某种原因,我无法显示emptyView
。
请帮忙吗?
代码:
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);
这是xml ....非常标准。:
<?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" />
感谢Faizan的反馈,带领我来到这里。这是解决方案:
View empty_view = inflater.inflate(R.layout.empty_view, null);
...
if (scoreViewAdapter.getCount() < 1)
fragView.addView(empty_view);
fragView.addView(listView);
答案 0 :(得分:2)
您必须使用LayoutInflater
对该xml进行充气,然后再调用
listView.setEmptyView(inflated_view);
您可以从onCreateView
覆盖的方法获取inflater
View inflated_view = inflater.inflate(R.layout.my_empty_layout,null);
我希望它能解决问题。
答案 1 :(得分:0)
我相信你延伸FragmentList
而非ListActivity
所以你实际上可以使用setEmptyView