【发布时间】:2020-03-29 22:55:49
【问题描述】:
Im 收到 Firebase DatabaseException“无法将 java.util.ArrayList 类型的对象转换为 com.example.firebasedeneme.Title 类型”
getValue() 出错了
a = FirebaseDatabase.getInstance().getReference("Users");
but.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
a.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if(dataSnapshot.child(e.getText().toString()).exists()){
String username = e.getText().toString();
User user = dataSnapshot.child(username).getValue(User.class);
我保存数据
FirebaseDatabase.getInstance().getReference("Users").child(user.getUsername()).setValue(user);
我的用户类看起来像
public class User implements Comparable<User>,Serializable{
private String username;
private String password;
private String title;
private int xp;
private int score;
private int level;
private int image;
private Stack<Title> titles;
public User() {}
public User(String username, String password, String title, int xp, int score, int level, int image, Stack<Title> titles){
this.username = username;
this.password = password;
this.level = level;
this.score = score;
this.xp = xp;
this.title = title;
this.image = image;
this.titles = titles;
}
我的 Title Class 看起来像
public class Title {
private int titleno;
private String title;
public Title(int titleno, String title){
this.title = title;
this.titleno = titleno;
}
public int getTitleno(){
return this.titleno;
}
public String getTitle(){
return this.title;
}
}
我该如何解决这个问题?
【问题讨论】:
-
请仅使用
android-studio标签来回答有关 Android Studio IDE 本身的问题。对于一般的 Android 编程问题,请使用android标签。 -
请将您的数据库结构添加为 JSON 文件或至少是屏幕截图。
-
我添加了我的数据库结构作为截图。
-
Mateo Hervas 的回答是正确的。您的
titles属性是 List 而不是Stack。 -
我尝试了 Mateo Hervas 的解决方案,但仍然遇到同样的问题。
标签: android firebase firebase-realtime-database