在我的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);
问题是,当活动开始时,即使我的适配器中有数据,也会显示
emptyView
。这发生的时间很短,而ListView
却没有出现。我该如何阻止这种情况的发生?如您所见,在设置listView.setEmptyView(emptyView);
之后,我已经设置了firebaseListAdapter
,但是没有运气。这是我的.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>
提前致谢!
最佳答案
如果您的适配器具有此方法,则可以使用此方法设置空视图,
adapter.registerDataSetObserver(new DataSetObserver() {
@Override
public void onChanged() {
super.onChanged();
TextView emptyView = (TextView) findViewById(R.id.empty_view);
listView.setEmptyView(emptyView);
}
});