gpt4 book ai didi

java - 从 Firebase 数据库读取数据失败

转载 作者:行者123 更新时间:2023-11-29 19:02:09 25 4
gpt4 key购买 nike

我的 Android 应用程序有问题,我无法在 Firebase 数据库中设置带有字符串的 textView。我想在我的 JSON 中使用字符串设置 TextView 的文本。

mDatabaseReference.child("Profilo").addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
for(DataSnapshot dataSnapshot1: dataSnapshot.getChildren()){
UserProfile mProfile = dataSnapshot1.getValue(UserProfile.class);
nomeProfilo.setText(mProfile.getNome());
}
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}

这是我的日志

com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.mysecurity.marco.mysecurty.UserProfile
at com.google.android.gms.internal.zzbtg.zze(Unknown Source:89)
at com.google.android.gms.internal.zzbtg.zzb(Unknown Source:145)
at com.google.android.gms.internal.zzbtg.zza(Unknown Source:0)
at com.google.firebase.database.DataSnapshot.getValue(Unknown Source:10)
at com.mysecurity.marco.mysecurty.MaterialUserActivity$3.onChildAdded(MaterialUserActivity.java:194)
at com.google.android.gms.internal.zzbox.zza(Unknown Source:33)
at com.google.android.gms.internal.zzbqx.zzZS(Unknown Source:2)
at com.google.android.gms.internal.zzbra$1.run(Unknown Source:63)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

你能帮帮我吗?

最佳答案

试试这个:

 mDatabaseReference.child("Profilo").addListenerForSingleValueEvent(new ValueEventListener() {//to retrieve data
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot dataSnapshot1: dataSnapshot.getChildren()){
String names=dataSnapshot1.child("nome").getValue().toString();
nomeProfilo.setText(names);


}
}
@Override
public void onCancelled(DatabaseError databaseError) {

}
});

或者您也可以这样做以防止使用循环进行迭代:

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
mDatabaseReference.child("Profilo").child(user.getUid()).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String names=dataSnapshot1.child("nome").getValue().toString();
nomeProfilo.setText(names);


}
}
@Override
public void onCancelled(DatabaseError databaseError) {

}
});

关于java - 从 Firebase 数据库读取数据失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48610676/

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