Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/396.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/222.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 获取Firestore中的ArrayList-Android_Java_Android_Firebase_Arraylist_Google Cloud Firestore - Fatal编程技术网

Java 获取Firestore中的ArrayList-Android

Java 获取Firestore中的ArrayList-Android,java,android,firebase,arraylist,google-cloud-firestore,Java,Android,Firebase,Arraylist,Google Cloud Firestore,我正在创建一个Android应用程序,它需要访问一个包含ArrayList的文档 我看了一下这个链接 我尽力了。这家伙甚至回答说我们应该做(ArrayList)documentSnapshot.get(“key”)。我尝试过这样做,但当我这样做时,我从Android Studio中得到了这个错误:未选中的强制转换:“java.lang.Object”到“java.util.ArrayList”。它还要求我泛化我的类 这是我的Firestore结构 这是我到目前为止的代码: DocumentRe

我正在创建一个Android应用程序,它需要访问一个包含ArrayList的文档

我看了一下这个链接

我尽力了。这家伙甚至回答说我们应该做
(ArrayList)documentSnapshot.get(“key”)
。我尝试过这样做,但当我这样做时,我从Android Studio中得到了这个错误:
未选中的强制转换:“java.lang.Object”到“java.util.ArrayList”
。它还要求我泛化我的类

这是我的Firestore结构

这是我到目前为止的代码:

DocumentReference docRef = firestore.collection("Users").document(fullMobile);
                docRef.get().addOnCompleteListener(task -> {
                    if (task.isSuccessful()) {
                        DocumentSnapshot document = task.getResult();
                        assert document != null;
                        // Data from FireStore
                        ArrayList<String> devices = (ArrayList<String>) document.get("Devices"); ----- This is the line which is wrong.
                        if (document.exists()) { // LOGIN
                            // Some Code
                        } else { // SIGNUP
                           // Some Code
                        }
                    } else {
                        toast("An Error Occured! Please Try Again");
                    }
                });
DocumentReference docRef=firestore.collection(“用户”).document(fullMobile);
docRef.get().addOnCompletListener(任务->{
if(task.issusccessful()){
DocumentSnapshot document=task.getResult();
断言文档!=null;
//来自FireStore的数据
ArrayList devices=(ArrayList)document.get(“devices”);——这是错误的一行。
如果(document.exists()){//LOGIN
//一些代码
}否则{//注册
//一些代码
}
}否则{
toast(“发生错误!请重试”);
}
});

我不知道该做什么,我知道。有人能帮我吗?

如果您的文档包含数据数组,请创建arraylist,然后添加项

ArrayList<String> devices = new ArrayList<>();
devices.add(document.get("Devices"));

如果您是初学者,只需将光标放在警告文本上,然后按Alt+Enter,Android studio将显示可用的选项来播放您的文档。选择一个适合您的项目。

如果您的文档包含数据数组,请创建arraylist,然后添加项目

ArrayList<String> devices = new ArrayList<>();
devices.add(document.get("Devices"));

如果您是初学者,只需将光标放在警告文本上,然后按Alt+Enter,Android studio将显示可用的选项来播放您的文档。选择一个适合您的。

添加代码的最后部分。请澄清您对术语的使用。Firestore中没有文档“是ArrayList”。文档可能包含数组。如果您共享Firestore结构的描述(或屏幕截图),然后共享从Firestore获取文档并提取试图加载到UI中的数组数据所使用的确切代码,则该功能将非常有用。您可以签出。然而,在数据库中,您有一个名为“Sample”的数组,而在代码中,您试图将其作为“设备”获取。对吗?请回复@AlexMamo@AlexMamo是的,数组是示例。你可以忽略“设备”这件事,我可以稍后更改,这不是问题所在。在这种情况下,androidLearner的答案解决问题了吗?添加一些代码的最后部分。请澄清你对术语的使用。Firestore中没有文档“是ArrayList”。文档可能包含数组。如果您共享Firestore结构的描述(或屏幕截图),然后共享从Firestore获取文档并提取试图加载到UI中的数组数据所使用的确切代码,则该功能将非常有用。您可以签出。然而,在数据库中,您有一个名为“Sample”的数组,而在代码中,您试图将其作为“设备”获取。对吗?请回复@AlexMamo@AlexMamo是的,数组是示例。你可以忽略“设备”的事情,我可以稍后更改,这不是问题所在。在这种情况下,androidLearner的答案解决问题了吗?我得到的错误与你应该尝试泛化我的类的错误相同。另外,addAll()方法仅适用于集合,而不适用于ArrayList@AnikethChavarecan您可以说您是如何在文档中设置字段的。如果您使用列表,我们可以将其作为Arraylist取回。如果您使用自定义类,则需要使用自定义类将其像列表一样取回这是使用自定义类CustomModelClass Model=document.toObject(CustomModelClass.class)时获取文档数据的正确方法;我得到的错误与您应该尝试泛化我的类的错误相同。另外,addAll()方法仅适用于集合,而不适用于ArrayList@AnikethChavarecan您可以说您是如何在文档中设置字段的。如果您使用列表,我们可以将其作为Arraylist取回。如果您使用自定义类,则需要使用自定义类将其像列表一样取回这是使用自定义类CustomModelClass Model=document.toObject(CustomModelClass.class)时获取文档数据的正确方法;