【发布时间】:2021-05-08 14:41:51
【问题描述】:
我正在从
Firebase加载一些内容,同时也在使用FirebaseRecyclerAdapter。
这是成功显示 Firebase 数据的函数:
public void showNotifications() {
databaseReference = FirebaseDatabase.getInstance().getReference("Notifications");
databaseReference.keepSynced(true);
options = new FirebaseRecyclerOptions.Builder<NotificationModel>()
.setQuery(FirebaseDatabase.getInstance().getReference("Notifications"), NotificationModel.class)
.build();
notifRecyclerView.setLayoutManager(new LinearLayoutManager(this));
notificationAdaper = new NotificationAdaper(options, NotificationActivity.this);
notifRecyclerView.setAdapter(notificationAdaper);
checkEmpty(); //will show empty image when recyclerView is empty
}
这是我的 checkEmpty() 方法:
private void checkEmpty() {
if (notificationAdaper.getItemCount() == 0) {
Log.e("Item count", String.valueOf(notificationAdaper.getItemCount()));
no_notif.setVisibility(View.VISIBLE);
} else {
no_notif.setVisibility(View.GONE);
}
}
这是用户界面代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
tools:context="com.devapps.NotificationActivity">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
app:theme="@style/ToolbarTheme">
<TextView
android:id="@+id/toolbarTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="1"
android:text="Notifications"
android:textColor="@android:color/white"
android:textSize="18sp"
android:textStyle="bold"
android:visibility="visible" />
</androidx.appcompat.widget.Toolbar>
<ImageView
android:id="@+id/no_notif"
android:layout_width="260dp"
android:layout_height="260dp"
android:visibility="gone"
android:layout_centerInParent="true"
android:scaleType="centerCrop"
android:src="@drawable/nonotification" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/notifRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/toolbar" />
</RelativeLayout>
即使 Firebase 数据库中有项目,空图像占位符也始终可见。如何解决。
每次我收到
E/Item count: 0,即使 Firebase 数据库中有项目。
【问题讨论】:
-
而更具体的答案,我们需要看到你的用户界面代码
-
已添加,请看@KamalNayan
-
查看答案.....我已经更新了
标签: android firebase-realtime-database android-recyclerview