[英]Display data from user uid with Firebase Database
I want to recover data from the uid and then display them in RecyclerView.我想从 uid 中恢复数据,然后在 RecyclerView 中显示它们。
I managed to recover the child/parent under Withdrawal thanks to the uid.多亏了 uid,我设法在 Withdrawal 下恢复了孩子/父母。
This is what it looks like in Java:这是 Java 中的样子:
referenceWithdraw = FirebaseDatabase.getInstance().getReference().child("Withdraw");
Query queryUid = referenceWithdraw.orderByChild("uid").equalTo(user.getUid());
queryUid.get().addOnCompleteListener(task -> {
if (task.isSuccessful()) {
for (DataSnapshot dataSnapshot : task.getResult().getChildren()) {
String id = dataSnapshot.child("id").getValue(String.class)
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
} else {
///
}
});
String id =...
retrieves the child value. String id =...
检索子值。
So far everything is fine except when I add this under the String id =...
:到目前为止一切都很好,除非我在
String id =...
下添加它:
referenceWithdraw.child(id).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
WithdrawProfile profile = snapshot.getValue(WithdrawProfile.class);
list.add(profile);
adapter.notifyDataSetChanged();
}
I have this error appearing in my Logcat:我的 Logcat 中出现了这个错误:
com.google.firebase.database.DatabaseException: Failed to convert a value of type java.lang.String to int
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertInteger(CustomClassMapper.java:364)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToPrimitive(CustomClassMapper.java:290)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToClass(CustomClassMapper.java:215)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToType(CustomClassMapper.java:179)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.access$100(CustomClassMapper.java:48)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper$BeanMapper.deserialize(CustomClassMapper.java:593)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper$BeanMapper.deserialize(CustomClassMapper.java:563)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertBean(CustomClassMapper.java:433)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToClass(CustomClassMapper.java:232)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertToCustomClass(CustomClassMapper.java:80)
at com.google.firebase.database.DataSnapshot.getValue(DataSnapshot.java:203)
at com.pckage.app.android.withdraw.Withdraw$1.onDataChange(Withdraw.java:62)
at com.google.firebase.database.core.ValueEventRegistration.fireEvent(ValueEventRegistration.java:75)
at com.google.firebase.database.core.view.DataEvent.fire(DataEvent.java:63)
at com.google.firebase.database.core.view.EventRaiser$1.run(EventRaiser.java:55)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:264)
at android.app.ActivityThread.main(ActivityThread.java:8306)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:632)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1049)
And finally here is my WithdrawProfile:最后是我的 WithdrawProfile:
public class WithdrawProfile {
private String date, email, status, id;
private int prize;
public WithdrawProfile(){}
public WithdrawProfile(String date, String email, String status, String id, int prize) {
this.date = date;
this.email = email;
this.status = status;
this.id = id;
this.prize = prize;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public int getPrize() {
return prize;
}
public void setPrize(int prize) {
this.prize = prize;
}
}
My database:我的数据库:
{
"Daily" : {
"KuTKHdv1qeYS1NeQVZlqYdZYyNi1" : {
"date" : "07/01/2022"
}
},
"Users" : {
"KuTKHdv1qeYS1NeQVZlqYdZYyNi1" : {
//Other value
"uid" : "KuTKHdv1qeYS1NeQVZlqYdZYyNi1"
}
},
"Withdraw" : {
"-MszSsu9rn6XzRMUNgTc" : {
"date" : "09/01/2022",
"id" : "-MszSsu9rn6XzRMUNgTc",
"email" : "test@gmail.com",
"prize" : "1000",
"statut" : "Pending",
"uid" : "KuTKHdv1qeYS1NeQVZlqYdZYyNi1"
}
}
}
Thank you for your time感谢您的时间
The problem is in reading this value from the database:问题在于从数据库中读取这个值:
"prize" : "1000",
In your Java class you define prize
as an int:在您的 Java class 中,您将
prize
定义为 int:
public int getPrize() {
return prize;
}
public void setPrize(int prize) {
this.prize = prize;
}
But in your database it is stored as a string value: "1000"
.但在您的数据库中,它存储为字符串值:
"1000"
。 Since they're not the same type, Firebase can't read the prize
value and throws an error.由于它们不是同一类型,因此 Firebase 无法读取
prize
值并引发错误。
The solution is to either store the prize as a (whole) number:解决方案是将奖品存储为(整数)数字:
"prize" : 1000,
Or to make it a string in your Java code too:或者让它成为你的 Java 代码中的字符串:
private string prize;
...
public string getPrize() {
return prize;
}
public void setPrize(string prize) {
this.prize = prize;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.