我正在尝试从 firebase 检索数据并将其显示在 recyclerview 上。我在尝试运行应用程序时遇到错误(下面的错误)。我想知道是循环导致了错误还是因为我得到了错误的值。有人可以帮我解决这个问题吗?我正在尝试获取存储在我的 Firebase 中的通知消息(“test”、“test2”)。将在下面显示数据库的图像。
错误
com.google.firebase.database.DatabaseException:无法将 java.lang.String 类型的对象转换为类型
通知片段.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) {
}
});
}
通知.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;
}
}
如果需要,我也可以发布我的适配器代码。
您似乎正在尝试将 a 直接转换String
为 a Notification
。Firebase 将返回一个可用于创建通知的字符串。
您可以尝试使用以下代码从 Firebase 的数据创建通知。
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
Notification menu = new Notification(postSnapshot.getValue(String.class));
notificationList.add(menu);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句