android - ListView 上的 setEmptyView 未在 android 应用程序中显示其 View ?

标签 android listview

我对 ListView 中的 setEmptyView 方法有疑问。

这是我的 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:

<?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" >

我的代码有什么问题? 当我有项目时,我会在列表中看到它们。但是当列表为空时,我看不到 TextView

最佳答案

您的 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>

这就是你的代码的样子

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);

关于android - ListView 上的 setEmptyView 未在 android 应用程序中显示其 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12483508/

相关文章:

android - 如果 key 存在,如何防止将数据添加到 Firebase

android - 我的 GridView/ListView 仅在 android api 18+ 中触摸后才会呈现

c# - ListView 取消选择 C# Metro

java - IntentService 中的广播接收器

Android Instagram API : how to retrieve pictures

java - 如何修复 Android 聊天中 ListView 中的此错误

android - 如何在单击按钮时实现带有一些图像的小弹出窗口

android - [Android]ListView剪切TextView文本

android - 在具有不同单元格高度的 ListView 中滚动

Android:MySQL 的 ListView 只显示最后一个元素