如何在Android中从Firebase获取List的值? [英] How to get values of List from Firebase in android?
问题描述
我从Firebase获取数据作为模型,并且在我的模型中,有一个 List< String>
,因此我可以将其发送到Firebase,但无法获取它:((>
我有一个模型,其中图像是列表图像;
CollectionReference colRef = db.collection("Vehicle");colRef.get().addOnCompleteListener(task-> {对于(QueryDocumentSnapshot文档:task.getResult()){Vehicle vehicle =新的Vehicle(document.getString("title"),document.getString("city"),document.get("image"),}
这给出一个错误,说我在列表的字符串中获取对象"document.get(图像")或字符串" document.getString(图像")"
没有"document.getList(图像")"
没有"document.getList("image")"
您是对的,在 现在,您可以通过将 I'm getting a data from Firebase as a model and in my model, there is a I have a model where an image is List Image; This gives an error saying that I'm getting Object "document.get("image")" or String "document.getString("image")" in sted of List There is no "document.getList("image")" there is no "document.getList("image")" You're right, there is no Returns the value at the field or null if the field doesn't exist. So if your Now you can create an object of 这篇关于如何在Android中从Firebase获取List的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! vehicleList
传递给构造函数来创建 Vehicle
类的对象.List<String>
so I can send it to Firebase but can't get it :(CollectionReference colRef = db.collection("Vehicle");
colRef.get().addOnCompleteListener(task -> {
for (QueryDocumentSnapshot document : task.getResult()) {
Vehicle vehicle = new Vehicle(document.getString("title"),
document.getString("city"),
document.get("image"), }
getList()
method in the QueryDocumentSnapshot class but because it extends DocumentSnapshot class, you can use get(String field) method:
image
property is of type array, you can simply get it as List
using the following line of code:List<String> vehicleList = (List<String>) document.get("image");
Vehicle
class by passing vehicleList
to the constructor.