gpt4 book ai didi

java - 从 firebase 检索并显示值

转载 作者:行者123 更新时间:2023-12-02 13:02:02 25 4
gpt4 key购买 nike

我正在尝试从 firebase 检索数据并将其显示在 recyclerview 上。我在尝试运行应用程序时遇到错误(下面的错误)。我想知道是循环导致了错误还是因为我得到了错误的值。有人能帮我解决这个问题吗?我正在尝试获取存储在我的 firebase 中的通知消息(“test”、“test2”)。下面将显示数据库的图像。

错误

com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type

数据库 enter image description here

NotificationFragment.java

   private void prepareNotification1() {
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
String userID = user.getUid();
mRef.child("customers").child(userID).child("Notification").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
System.out.println(postSnapshot.getValue());
// System.out.println(dataSnapshot.getKey());
// Log.d("value", dataSnapshot.getKey());
Notification menu = postSnapshot.getValue(Notification.class);
notificationList.add(menu);
}
mAdapter.notifyDataSetChanged();
}

@Override
public void onCancelled(DatabaseError databaseError) {

}
});

}

Notification.java

public class Notification {
private String notification;




public Notification(){
}

public Notification(String notification){
this.notification = notification;
}

public String getNotification() {
return notification;
}

public void setNotification(String notification) {
this.notification = notification;
}
}

如果需要,我也可以发布我的适配器代码。

最佳答案

您似乎正在尝试将String直接转换为Notification。Firebase 将返回一个字符串,您可以使用它来创建通知。

您可以尝试使用以下代码从 Firebase 的数据创建通知。

for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
Notification menu = new Notification(postSnapshot.getValue(String.class));
notificationList.add(menu);
}

关于java - 从 firebase 检索并显示值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44261617/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com