我想根据子节点的特定键访问用户中的子项,并且已经在我的函数getcount()中传递了该键,但是当我使用该引用键访问用户时,它会显示com.google.firebase .database.DatabaseException:无法将java.lang.Long类型的值转换为String。
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) {
}
});
}
我的用户模型类
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;
}
}
答案 0 :(得分:2)
正如我在您的数据库中看到的那样,在"
中的一个文档中截取了您的数量和电话号码,而在另外一个文档中却很长。手动更改
答案 1 :(得分:1)
将您的phone
和count
属性从String
更改为long
,因为您的数据库将其包含为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;
}
}