Reputation: 67
I want to access child item in users according to particular key of child node and I have passed that key in my function getcount() but when I am accessing the user with that referal key it is showing up com.google.firebase.database.DatabaseException: Failed to convert value of type java.lang.Long to String.
I have used the following code but it is not working properly
private void getCount(final String referalKey) {
DatabaseReference myref=FirebaseDatabase.getInstance().getReference().child("users").child(referalKey);
myref.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
user u=dataSnapshot.getValue(user.class);
Toast.makeText(Home.this,u.getCount(),Toast.LENGTH_SHORT).show();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
my user model class
public class user {
public String name;
public String phone;
public String refphone;
public String address;
public String count;
public user() {
// Default constructor required for calls to DataSnapshot.getValue(User.class)
}
public String getCount() {
return count;
}
public void setCount(String count) {
this.count = count;
}
public user(String name, String phone, String refphone, String address, String count) {
this.name = name;
this.phone = phone;
this.refphone = refphone;
this.address = address;
this.count = count;
}
public void setName(String name) {
this.name = name;
}
public void setPhone(String phone) {
this.phone = phone;
}
public void setRefphone(String refphone) {
this.refphone = refphone;
}
public void setAddress(String address) {
this.address = address;
}
public String getName() {
return name;
}
public String getPhone() {
return phone;
}
public String getRefphone() {
return refphone;
}
public String getAddress() {
return address;
}
}
Upvotes: 0
Views: 44
Reputation: 15423
Change your phone
and count
property from String
to long
as your database contain it as long
public class user {
public String name;
public long phone;
public String refphone;
public String address;
public long count;
public user() {
// Default constructor required for calls to DataSnapshot.getValue(User.class)
}
public long getCount() {
return count;
}
public void setCount(long count) {
this.count = count;
}
public user(String name, long phone, String refphone, String address, long count) {
this.name = name;
this.phone = phone;
this.refphone = refphone;
this.address = address;
this.count = count;
}
public void setName(String name) {
this.name = name;
}
public void setPhone(long phone) {
this.phone = phone;
}
public void setRefphone(String refphone) {
this.refphone = refphone;
}
public void setAddress(String address) {
this.address = address;
}
public String getName() {
return name;
}
public long getPhone() {
return phone;
}
public String getRefphone() {
return refphone;
}
public String getAddress() {
return address;
}
}
Upvotes: 1
Reputation: 6919
As i can see in your database Screenshot your count and phone number in one of the document it's in "
and on other it's long. Change it manually
Upvotes: 2