Reputation: 51
I want to recover data from the uid and then display them in RecyclerView.
I managed to recover the child/parent under Withdrawal thanks to the uid.
This is what it looks like in 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.
So far everything is fine except when I add this under the
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 :
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 :
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" : "[email protected]",
"prize" : "1000",
"statut" : "Pending",
"uid" : "KuTKHdv1qeYS1NeQVZlqYdZYyNi1"
}
}
}
Thank you for your time
Upvotes: 0
Views: 274
Reputation: 598765
The problem is in reading this value from the database:
"prize" : "1000",
In your Java class you define prize
as an 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"
. Since they're not the same type, Firebase can't read the prize
value and throws an error.
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:
private String prize;
...
public String getPrize() {
return prize;
}
public void setPrize(String prize) {
this.prize = prize;
}
Upvotes: 1