[英]Child List from Firebase with android
I'm trying to get the value of my firebase tree 我正在尝试获取Firebase树的价值
I got a class get_categories 我有一个课程get_categories
public class get_categories {
private String Name;
public get_categories(){
}
public String getName(){
return Name;
}
}
Then I declared the firebase 然后我宣布了火力基地
mRef = new Firebase("https://...../Users/Categories");
And try to retrieve my data with OnChildEventListener 并尝试使用OnChildEventListener检索我的数据
mRef.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Log.e("Count " ,""+dataSnapshot.getChildrenCount());
//Log.e("VALUES " ,""+dataSnapshot.child("Details").getValue());
get_categories getCatName = dataSnapshot.child("Details").getValue(get_categories.class);
Log.e("NAME " ,""+getCatName.getName());
}
@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(FirebaseError firebaseError) {
}
});
But I got an error 但是我有一个错误
Caused by: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "Name" (class com.app.pierre.myapp.get_categories), not marked as ignorable (0 known properties: ]) at [Source: java.io.StringReader@9d0f705; 引起原因:com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:无法识别的字段“名称”(类com.app.pierre.myapp.get_categories),在[来源:java。]中未标记为可忽略(0个已知属性:)。 io.StringReader@9d0f705; line: 1, column: 10] (through reference chain: com.app.pierre.myapp.get_categories["Name"]) 行:1,列:10](通过参考链:com.app.pierre.myapp.get_categories [“ Name”])
What am I doing wrong ? 我究竟做错了什么 ?
I used this case as reference: https://www.firebase.com/docs/android/guide/retrieving-data.html 我将此案例用作参考: https : //www.firebase.com/docs/android/guide/retrieving-data.html
Well first change your Model like this, 首先,像这样更改您的模型,
public class GetCategories {
private String Name;
public GetCategories(){
}
public GetCategories(String name){
this.Name = name;
}
public String getName(){
return Name;
}
}
and Push data using Model class Constructor.. 和使用模型类构造器推送数据。
GetCategories model = new GetCategories("yourValues");
mReference.child("Details").push().setValue(model);
and Now you can get it like this way.. 现在您可以通过这种方式获得它。
mRef.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
for (DataSnapshot eventSnapshot : dataSnapshot.getChildren()) {
GetCategories newPost = eventSnapshot.getValue(GetCategories.class);
Log.e("NAME " ,""+ newPost.getName());
}
}
@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(FirebaseError firebaseError) {
}
});
The name of the property on your JSON starts with an uppercase letter. JSON上的属性名称以大写字母开头。 But by the rules of JavaBean properties, getName()
correspond to a property called name
(with a lowercase n). 但是根据JavaBean属性的规则, getName()
对应于一个名为name
的属性(小写的n)。 So the two don't match up. 所以两个不匹配。
One way to get this working is to not use a setter, but instead use a public field: 一种有效的方法是不使用setter,而使用公共字段:
public class get_categories {
public String Name;
}
With this you're bypassing the JavaBean logic and get a direct mapping. 这样,您就可以绕过JavaBean逻辑并获得直接映射。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.