我有一个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);