如何在Android中从Firebase获取List的值? [英] How to get values of List from Firebase in android?

查看:31
本文介绍了如何在Android中从Firebase获取List的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从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")"

您是对的,在getList()方法/QueryDocumentSnapshot"rel =" nofollow noreferrer> QueryDocumentSnapshot 类,但因为它扩展了 DocumentSnapshot 类,您可以使用

现在,您可以通过将 vehicleList 传递给构造函数来创建 Vehicle 类的对象.

I'm getting a data from Firebase as a model and in my model, there is a List<String> so I can send it to Firebase but can't get it :(

I have a model where an image is List Image;

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"),                                        }

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 getList() method in the QueryDocumentSnapshot class but because it extends DocumentSnapshot class, you can use get(String field) method:

Returns the value at the field or null if the field doesn't exist.

So if your 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");

Now you can create an object of Vehicle class by passing vehicleList to the constructor.

这篇关于如何在Android中从Firebase获取List的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆