我有一个返回ListView的ListFragment。如果列表中没有项目,我想改为显示替代TextView。据说你可以在XML中添加一个 TextView android:id =“@ android:id / empty”元素。
但是...
添加包含两者的FrameLayout ListView和TextView可以预见到“FrameLayout无法转换为ListView”。
我不能只是添加TextView元素。 “必须正确完成根元素之后的文档中的标记。”
我无法切换到查看而不是 ListView ,因为它不再是列表。
......我该怎么做呢?
代码:
@Override
public View onCreateView ( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState ) {
adapter = new ArrayAdapter<Project> ( getActivity (), android.R.layout.simple_list_item_1 );
arrayList = ProjectManager.getProjectList ( getActivity () );
for ( Project p : arrayList ) {
if ( p.getStatus () == Project.ONGOING ) {
adapter.add ( p );
}
}
ListView listView = ( ListView ) inflater.inflate ( R.layout.list, container, false );
listView.setAdapter ( adapter );
return listView;
}
XML:
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
答案 0 :(得分:1)
我有一个返回ListView的ListFragment。如果列表中没有项目,我想改为显示替代TextView。据说你可以在XML中添加一个TextView android:id =“@ android:id / empty”元素。
你可以。
添加包含ListView和TextView的FrameLayout可以预见到“FrameLayout无法转换为ListView”。
这是因为您的代码中存在错误,您认为inflate()
的结果是ListView
。将其更改为使用findViewById()
在充气布局中找到 ListView
。
答案 1 :(得分:0)
添加ID为空的TextView:
<TextView android:id="@android:id/empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/emptyt_message" />
到相同的布局
答案 2 :(得分:0)
请查看以下方法:
为了使其有用,您的片段应该包含一个容器(即FrameLayout
),其中包含ListView
和TextView
的空信息。
在夸大布局时,请指定文本视图的视图ID。将此文本视图的初始可见性设置为INVISIBLE
。文本视图的可见性将根据适配器中的元素数自动更新。