我正在尝试从我已经创建的firebase数据库中获取特定值。我跟着这个youtube tutorial和另一个similar question,这与我想要实现的目标非常相关。
这就是我的Firebase数据库的外观Database Tree
我正在尝试从此数据库中获取所有单个项目,例如Name,Snippet,Lat,Long。
这是我到目前为止所尝试的内容。现在我只试图获得名称项目
在Oncreate方法中:
final TextView nametext = (TextView)findViewById(R.id.name);
Firebase.setAndroidContext(this);
final Firebase ref = new Firebase("https://fir-with-maps.firebaseio.com/Group 2");
final List<PlumbersList> listofplumbers = new ArrayList<>();
ref.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
PlumbersList plumberslist = dataSnapshot.getValue(PlumbersList.class);
listofplumbers.add(plumberslist);
String name = plumberslist.Name;
nametext.setText(name);
}
@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) {
}
});
}
PlumberList类
public class PlumbersList {
String Name;
String Snippet;
String Lat;
String Long;
public PlumbersList() {
}
public PlumbersList(String name, String snippet, String lat, String aLong) {
Name = name;
Snippet = snippet;
Lat = lat;
Long = aLong;
}
public String getName() {
return Name;
}
public String getSnippet() {
return Snippet;
}
public String getLat() {
return Lat;
}
public String getLong() {
return Long;
}
应用程序在几秒钟后崩溃。以下是logcat的错误
09-09 23:41:19.381 1375-1375/digiart.mapwithfirebase E/UncaughtException: com.firebase.client.FirebaseException: Failed to bounce to type
at com.firebase.client.DataSnapshot.getValue(DataSnapshot.java:183)
at digiart.mapwithfirebase.checkingfirebase$1.onChildAdded(checkingfirebase.java:36)
at com.firebase.client.core.ChildEventRegistration.fireEvent(ChildEventRegistration.java:48)
at com.firebase.client.core.view.DataEvent.fire(DataEvent.java:45)
at com.firebase.client.core.view.EventRaiser$1.run(EventRaiser.java:38)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:210)
at android.app.ActivityThread.main(ActivityThread.java:5833)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1113)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:879)
Caused by: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "Snippet" (class digiart.mapwithfirebase.PlumbersList), not marked as ignorable (0 known properties: ])
at [Source: java.io.StringReader@297f6a4e; line: 1, column: 13] (through reference chain: digiart.mapwithfirebase.PlumbersList["Snippet"])
at com.fasterxml.jackson.databind.DeserializationContext.reportUnknownProperty(DeserializationContext.java:555)
at com.fasterxml.jackson.databind.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:708)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownProperty(BeanDeserializerBase.java:1160)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:315)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:121)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2888)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2034)
at com.firebase.client.DataSnapshot.getValue(DataSnapshot.java:181)
at digiart.mapwithfirebase.checkingfirebase$1.onChildAdded(checkingfirebase.java:36)
at com.firebase.client.core.ChildEventRegistration.fireEvent(ChildEventRegistration.java:48)
at com.firebase.client.core.view.DataEvent.fire(DataEvent.java:45)
at com.firebase.client.core.view.EventRaiser$1.run(EventRaiser.java:38)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:210)
at android.app.ActivityThread.main(ActivityThread.java:5833)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1113)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:879)
09-09 23:41:19.711 1375-1375/digiart.mapwithfirebase E/AndroidRuntime: FATAL EXCEPTION: main Process: digiart.mapwithfirebase, PID: 1375
com.firebase.client.FirebaseException: Failed to bounce to type
at com.firebase.client.DataSnapshot.getValue(DataSnapshot.java:183)
at digiart.mapwithfirebase.checkingfirebase$1.onChildAdded(checkingfirebase.java:36)
at com.firebase.client.core.ChildEventRegistration.fireEvent(ChildEventRegistration.java:48)
at com.firebase.client.core.view.DataEvent.fire(DataEvent.java:45)
at com.firebase.client.core.view.EventRaiser$1.run(EventRaiser.java:38)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:210)
at android.app.ActivityThread.main(ActivityThread.java:5833)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1113)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:879)
Caused by: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "Snippet" (class digiart.mapwithfirebase.PlumbersList), not marked as ignorable (0 known properties: ])
at [Source: java.io.StringReader@297f6a4e; line: 1, column: 13] (through reference chain: digiart.mapwithfirebase.PlumbersList["Snippet"])
at com.fasterxml.jackson.databind.DeserializationContext.reportUnknownProperty(DeserializationContext.java:555)
at com.fasterxml.jackson.databind.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:708)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownProperty(BeanDeserializerBase.java:1160)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:315)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:121)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2888)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2034)
at com.firebase.client.DataSnapshot.getValue(DataSnapshot.java:181)
at digiart.mapwithfirebase.checkingfirebase$1.onChildAdded(checkingfirebase.java:36)
at com.firebase.client.core.ChildEventRegistration.fireEvent(ChildEventRegistration.java:48)
at com.firebase.client.core.view.DataEvent.fire(DataEvent.java:45)
at com.firebase.client.core.view.EventRaiser$1.run(EventRaiser.java:38)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:210)
at android.app.ActivityThread.main(ActivityThread.java:5833)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1113)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:879)
我是Android编码的新手,所以我可能在这里犯了一些非常愚蠢的错误。
答案 0 :(得分:1)
根据您的数据树,您正在阅读“第2组”,即您正在下载“第2组”下的所有内容,包括“1”。你想在这里实现的是主要问题。会有数据“2”,“3”,“4”等吗?这些信息是否需要检索?
不,我只打算阅读“1”中的信息
- 根据您将textview设置为单个名称,我假设您只需要第一个项目。因此,您应该使用addValueEventListener
代替,并直接从https://fir-with-maps.firebaseio.com/Group 2/1
// add child "1"
ref.child("1").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(final DataSnapshot dataSnapshot) {
PlumbersList plumberslist = dataSnapshot.getValue(PlumbersList.class);
}
});
是的,我需要“第2组”下的所有内容 - 然后你只需要深入一级。
ref.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
DataSnapshot childSnapshot = dataSnapshot.getValue(); // get the values of "1"
PlumbersList plumberslist = childSnapshot.getValue(PlumbersList.class);
listofplumbers.add(plumberslist);
String name = plumberslist.Name;
nametext.setText(name);
}
});
答案 1 :(得分:1)
建议/问题(?)为什么对象名为PlumbersList?它看起来更像是一个水管工对象。
让每个水管工进入节点&#34;组2&#34; (1,2,3 .... n),你需要迭代该节点上的所有孩子
ArrayList<Plumber> mPlumbersList = new ArrayList<>();
ref = FirebaseDatabase.getInstance().getReference().child("group 2");
ref.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
for (DataSnapshot data : dataSnapshot.getChildren())
{
Plumber plumber = data.getValue(Plumber.class);
mPlumbersList.add(plumber);
}
// Notify the adapter after the foreach loop ends, if this list is backing one
mAdapter.notifyDataSetChanged();
}
...
});