ListView 上的 setEmptyView 未在 Android 应用程序中显示其视图?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12483508/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
setEmptyView on ListView not showing its view in a android app?
提问by gurehbgui
I have a problem with the setEmptyView method from a ListView.
我对 ListView 的 setEmptyView 方法有问题。
Here is my Java code:
这是我的Java代码:
ListView view = (ListView)findViewById(R.id.listView1);
view.setEmptyView(findViewById(R.layout.empty_list_item));
ArrayAdapter<Session> adapter1 = new ArrayAdapter<Session>(this, android.R.layout.simple_list_item_1,
android.R.id.text1, MainController.getInstanz().getItems());
view.setAdapter(adapter1);
empty_list_item:
empty_list_item:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/emptyList" >
</TextView>
listview:
列表显示:
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
What is wrong with my code?
When I have items I see them in the list. But when the list is empty I don't see the TextView
.
我的代码有什么问题?当我有项目时,我会在列表中看到它们。但是当列表为空时,我看不到TextView
.
采纳答案by gurehbgui
the problem is, i have to do a addContentView:
问题是,我必须做一个 addContentView:
View empty = getLayoutInflater().inflate(R.layout.empty_list_item, null, false);
addContentView(empty, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
view.setEmptyView(empty);
now its fine
现在很好
回答by mmbrian
Your TextView should be placed right under the ListView item with its visibility set to gone (android:visibility="gone"), do not place it in another layout. This is how your main layout would look like
您的 TextView 应该放在 ListView 项的正下方,并将其可见性设置为消失 (android:visibility="gone"),不要将其放置在其他布局中。这是您的主要布局的样子
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/listViewFangbuch"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
<TextView
android:id="@+id/empty_list_item"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"
android:text="@string/emptyList" >
</TextView>
</LinearLayout>
And this is how your code might look like
这就是你的代码的样子
ListView view = (ListView)findViewById(R.id.listViewFangbuch);
view.setEmptyView(findViewById(R.id.empty_list_item));
ArrayAdapter<Session> adapter1 = new ArrayAdapter<Session>(this, android.R.layout.simple_list_item_1,
android.R.id.text1, MainController.getInstanz().getItems());
view.setAdapter(adapter1);
回答by danigonlinea
For me, none of this answers worked for me. What I had to do was add the empty view manually (inflated) to the "parent" view of the Listview:
对我来说,这些答案都不适合我。我必须做的是将空视图手动(膨胀)添加到 Listview 的“父”视图:
ListView my_list = (ListView) findViewById(R.id.my_list);
View emptyView = getLayoutInflater().inflate(R.layout.empty_view,null);
((ViewGroup)my_list.getParent()).addView(emptyView);
listView.setEmptyView(emptyView);
回答by Sam
The simplest way to use setEmptyView()
is with your "empty" TextView and ListView in the same layout like this:
最简单的使用方法setEmptyView()
是将“空”TextView 和 ListView 放在相同的布局中,如下所示:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/empty_list_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/emptyList"
android:visibility="gone" />
</LinearLayout>
Now you can use:
现在您可以使用:
view.setEmptyView(findViewById(R.id.empty_list_item));
回答by Harvey
After trying all of the solutions presented here and experimenting, I believe the code below is the best method to use when your ListView and empty View are not side-by-side in a layout with the automatic ids set.
在尝试了此处提供的所有解决方案并进行试验后,我相信当您的 ListView 和空 View 在设置了自动 id 的布局中没有并排时,下面的代码是最好的使用方法。
ViewGroup parentGroup = (ViewGroup)getListView().getParent();
View empty = getActivity().getLayoutInflater().inflate(R.layout.list_empty_view,
parentGroup,
false);
parentGroup.addView(empty);
getListView().setEmptyView(empty);
Reasoning:
推理:
- By passing the parent group into inflate, the layout_* attributes of your empty view layout are respected
- By not attaching the view in inflate (the false parameter), the empty view is returned. Otherwise, inflate would return the parent requiring us to use parentGroup.findViewById(empty_view_id) to get the empty view for use with setEmptyView(). Here we avoid the extra lookup, the need for another id, and the need to expose that id in our code. If we didn't need to preserve the reference to empty, telling inflate to attach would be the correct action removing the need for the addView call.
- An anti-pattern, addContentView(), is not the correct approach. It will appear equivalent as long as your ListView occupies all of your Activity's visible space. Instead of your empty view being a sibling to your ListView, it ends up being a sibling to your other root-level layouts in your Activity. It appears to work because it's floating (z-order) on top of all other Views in the activity. See: Activity.addContentView(View) == ViewGroup.addContentView(View)?
- 通过将父组传递给 inflate,您的空视图布局的 layout_* 属性得到尊重
- 通过不在 inflate 中附加视图(false 参数),将返回空视图。否则,inflate 将返回父级,要求我们使用 parentGroup.findViewById(empty_view_id) 来获取与 setEmptyView() 一起使用的空视图。在这里,我们避免了额外的查找、需要另一个 id 以及需要在我们的代码中公开该 id。如果我们不需要保留对空的引用,告诉 inflate 附加将是正确的操作,消除对 addView 调用的需要。
- 反模式 addContentView() 不是正确的方法。只要您的 ListView 占据您活动的所有可见空间,它就会看起来是等效的。你的空视图不是你的 ListView 的兄弟,它最终成为你 Activity 中其他根级布局的兄弟。它似乎有效,因为它在活动中的所有其他视图之上浮动(z 顺序)。请参阅:Activity.addContentView(View) == ViewGroup.addContentView(View)?
回答by Dave
The core of the problem is that the AdapterView class does not actually add the view you pass to setEmptyView() to any container. It just changes the visibility. There are lots of ways to put your "empty view" object into a container, as the other answers describe.
问题的核心是 AdapterView 类实际上并没有将您传递给 setEmptyView() 的视图添加到任何容器中。它只是改变了可见性。正如其他答案所描述的那样,有很多方法可以将“空视图”对象放入容器中。
The documentation for AdapterView should really be updated to reflect this, since it is not obvious.
AdapterView 的文档确实应该更新以反映这一点,因为它并不明显。
回答by Nima G
Create a layout in XML that specifies both the ListView
and the empty view you want to use. If you give them IDs of @android:id/list
and @android:id/empty
, android will automatically use the view with id:empty
when list is empty.
在 XML 中创建一个布局,指定ListView
要使用的视图和空视图。如果你给他们的标识@android:id/list
和@android:id/empty
,Android将自动使用这样的观点id:empty
时,列表为空。
回答by cesards
There are some correct solutions, but I think this is the best approach!
有一些正确的解决方案,但我认为这是最好的方法!
View emptyView = getLayoutInflater().inflate(R.layout.empty_list_cart, null);
addContentView(emptyView, listView.getLayoutParams()); // You're using the same params as listView
listView.setEmptyView(emptyView);
listView.setAdapter(adapter);
回答by user2162746
also these two methods helped me with displaying empty view:
这两种方法也帮助我显示空视图:
adapter.notifyDataSetChanged();
list.invalidateViews();
回答by Sazzad Hissain Khan
Just use,
就用,
View emptyView = getLayoutInflater().inflate(R.layout.empty_view, null);
((ViewGroup)listView.getParent()).addView(emptyView);
Instead of listView.setEmptyView(emptyView);
代替 listView.setEmptyView(emptyView);