android - 如何在使用 Android 填充 ListView 时停止显示 TextView 的 setEmptyView?

标签 android listview listadapter

在我的 MainActivty 中,我使用 FirebaseListAdapter 显示来自 Firebase 数据库的项目列表,如下所示:

firebaseListAdapter = new FirebaseListAdapter<String>(MainActivity.this, String.class, R.layout.list, ref) {
@Override
protected void populateView(final View v, final String listName, int position) {
       ((TextView )v.findViewById(R.id.list_name)).setText(listName);
    }
};
listView = (ListView) findViewById(R.id.list_view);
listView.setAdapter(firebaseListAdapter);

TextView emptyView = (TextView) findViewById(R.id.empty_view);
listView.setEmptyView(emptyView);

问题是,当 Activity 开始时,即使我的适配器中有数据,也会显示 emptyView。这会持续很短的时间,而 ListView 会被填充然后消失。我怎样才能阻止这种情况发生?如您所见,我在设置 firebaseListAdapter 之后设置了 listView.setEmptyView(emptyView); 但没有成功。这是我的 .xml 文件的样子:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/coordinator_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
    android:id="@+id/empty_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:visibility="gone"/>

    <ListView
    android:id="@+id/list_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
</android.support.design.widget.CoordinatorLayout>

提前致谢!

最佳答案

如果你的适配器有这个方法,你可以在这个方法中设置空 View ,

adapter.registerDataSetObserver(new DataSetObserver() {

            @Override
            public void onChanged() {
                super.onChanged();

              TextView emptyView = (TextView) findViewById(R.id.empty_view);
               listView.setEmptyView(emptyView);

            }
        });

关于android - 如何在使用 Android 填充 ListView 时停止显示 TextView 的 setEmptyView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43071802/

相关文章:

java - 在线程中运行函数或在函数中使用线程

c# - 如何仅列出位于根节点正下方的 XML 节点

android - 使用 customAdapter 从 Listview 获取所有选中项目的列表

android - Recyclerview 和处理不同类型的行膨胀

java - 更改内部适配器内容后在 ListView 上调用刷新

android - 为什么 fragment 中 `by lazy` 委托(delegate)的这种奇怪行为

android - RxJava 类立即调度程序

android - 找不到模块 "./app.module.ngfactory"

android - 在 Android 中的自定义数组适配器中向 EditText 添加监听器

java - AsyncTask 更改 BaseAdapter (ListAdapter) 中的 ViewGroup 属性