[英]Get part of document into object from Firebase Firestore
I'm creating one document with this data:我正在用这些数据创建一个文档:
{
"fields":{
"meta":{
"mapValue":{
"fields":{
"semana":{
"stringValue":"Semana XX"
},
"fecha":{
"stringValue":"20191209"
},
"titulo":{
"stringValue":"Lunes II de Adviento"
},
"mensaje":{
"stringValue":""
},
"tiempo":{
"integerValue":1
}
}
}
},
"lh":{
"mapValue":{
"fields":{
"0":{
"referenceValue":"projects/miproject/databases/(default)/documents/es/v1/liturgia/lh/0/0108022"
},"1":{
"referenceValue":"projects/miproject/databases/(default)/documents/es/v1/liturgia/lh/1/0108022"
},"2":{
"referenceValue":"projects/miproject/databases/(default)/documents/es/v1/liturgia/lh/2/0100022"
},"3":{
"referenceValue":"projects/miproject/databases/(default)/documents/es/v1/liturgia/lh/3/0100022"
},"4":{
"referenceValue":"projects/miproject/databases/(default)/documents/es/v1/liturgia/lh/4/0100022"
},"5":{
"referenceValue":"projects/miproject/databases/(default)/documents/es/v1/liturgia/lh/5/0100022"
},"6":{
"referenceValue":"projects/miproject/databases/(default)/documents/es/v1/liturgia/lh/6/0100022"
}
}
}
}
}
}
The document is created without a problem.创建文档没有问题。
Now, in Android, I need to fill two types of objects:现在,在 Android 中,我需要填充两种类型的对象:
a.一种。 With the key
meta
I need to populate one object of type MetaLiturgia
, and使用关键
meta
我需要填充一个MetaLiturgia
类型的MetaLiturgia
,并且
b.湾I take one of the references to populate another object.
我使用其中一个引用来填充另一个对象。
My problem is in a , I can't populate the MetaLiturgia
object.我的问题是在,我不能填充
MetaLiturgia
对象。
DocumentReference calRef = db.collection(CALENDAR_PATH).document(fechaYY).collection(fechaMM).document(fechaDD);
calRef.addSnapshotListener(new EventListener<DocumentSnapshot>() {
@Override
public void onEvent(@Nullable DocumentSnapshot calSnapshot,
@Nullable FirebaseFirestoreException e) {
if (calSnapshot != null && calSnapshot.exists()) {
Log.d(TAG,calSnapshot.toString());
Log.d("meta",calSnapshot.get("meta").toString());
/* (a) Error here, line 120 is below line*/
mMeta = (MetaLiturgia) calSnapshot.getData();
DocumentReference dataRef=calSnapshot.getDocumentReference("lh.1");
if (e != null || dataRef==null) {
launchVolley();
return;
}
dataRef.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
@Override
public void onSuccess(DocumentSnapshot dataSnapshot) {
/*(b) No problem here*/
mBreviario = dataSnapshot.toObject(Breviario.class);
showData();
}
});
} else {
launchVolley();
}
}
});
I can see the data in the Log:我可以在日志中看到数据:
D/meta: {fecha=20191209, tiempo=1, semana=Semana XX, titulo=Lunes II de Adviento, mensaje=}
But I'm having this error:但我有这个错误:
2019-12-09 10:44:52.233 25121-25121/org.my.app E/AndroidRuntime: FATAL EXCEPTION: main Process: org.my.app, PID: 25121 java.lang.ClassCastException: java.util.HashMap cannot be cast to org.my.app.model.MetaLiturgia at org.my.app.activities.OficioActivity$1.onEvent(OficioActivity.java:120)
2019-12-09 10:44:52.233 25121-25121/org.my.app E/AndroidRuntime:致命异常:主进程:org.my.app,PID:25121 java.lang.ClassCastException:java.util.HashMap 不能在 org.my.app.activities.OficioActivity$1.onEvent(OficioActivity.java:120) 上转换为 org.my.app.model.MetaLiturgia
How I can do this?我怎么能做到这一点?
You are getting the following exception:您收到以下异常:
E/AndroidRuntime: FATAL EXCEPTION: main Process: org.my.app, PID: 25121 java.lang.ClassCastException: java.util.HashMap cannot be cast to org.my.app.model.MetaLiturgia
E/AndroidRuntime:致命异常:主进程:org.my.app,PID:25121 java.lang.ClassCastException:java.util.HashMap 无法转换为 org.my.app.model.MetaLiturgia
When using the following line of code:使用以下代码行时:
mMeta = (MetaLiturgia) calSnapshot.getData();
Because when you are calling calSnapshot.getData()
the type of object that is returned is a HashMap
and not a MetaLiturgia
and there is no way in Java you can cast an object of type HashMap
to MetaLiturgia
.因为当您调用
calSnapshot.getData()
,返回的对象类型是HashMap
而不是MetaLiturgia
并且在 Java 中无法将HashMap
类型的对象转换为MetaLiturgia
。 Since that object is a HashMap
, you should get it accordingly.由于该对象是
HashMap
,因此您应该相应地获取它。 But please also note that your fields
property is a HashMap
of HashMaps
.但也请注意,您的
fields
属性是一个HashMap
的HashMaps
。 So you should iterate twice in order to get the desired data.所以你应该迭代两次以获得所需的数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.