从Firebase检索一个ArrayList。

huangapple go评论78阅读模式
英文:

Retrieve an ArrayList from Firebase

问题

以下是翻译好的部分:

我在Firebase中有一个对象的ArrayList,我想要检索它。
我有两个ArrayList,一个是String,一个是Grade。当我尝试使用对象的数组时出现问题:

> java.lang.ClassCastException: java.util.HashMap 无法强制转换为 com.example.gradecalculator.Grade

mDocRef:

private DocumentReference mDocRef = FirebaseFirestore.getInstance().document("myData/Arrays");

上传数组:

    Map<String, Object> dataToSave = new HashMap<String, Object>();
    dataToSave.put("StringsArray", stringGrades); // 保存String数组
    dataToSave.put("ObjectsArray", grades); // 保存对象数组
    mDocRef.set(dataToSave).addOnSuccessListener(new OnSuccessListener<Void>() {
        @Override
        public void onSuccess(Void aVoid) {
            Log.d(TAG, "文档已保存!");
            Toast.makeText(getApplicationContext(), "列表已成功保存!", Toast.LENGTH_SHORT).show();

下载数组:

        mDocRef.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
        @Override
        public void onSuccess(DocumentSnapshot documentSnapshot) {
            isListEmpty = false;
            if (documentSnapshot.exists()) {
                Map<String, Object> dataToLoad = (HashMap<String, Object>) documentSnapshot.getData();
                stringGrades = (ArrayList<String>) dataToLoad.get("StringsArray");
                grades = (ArrayList<Grade>) dataToLoad.get("ObjectsArray");

                for (Grade grade : grades) {    // &lt;------------在这里失败
                    System.out.println(grade.toString());
                }

第一次尝试使用Firebase。似乎它成功保存了数据到服务器。有什么建议吗?

英文:

I have an ArrayList of Objects in Firebase and I would like to retreive it.
I have 2 array lists. One String and one Grade. When I try to use the array of objects it has a problem:

> java.lang.ClassCastException: java.util.HashMap cannot be cast to com.example.gradecalculator.Grade

mDocRef:

private DocumentReference mDocRef = FirebaseFirestore.getInstance().document(&quot;myData/Arrays&quot;);

Uploading the arrays:

    Map&lt;String, Object&gt; dataToSave = new HashMap&lt;String, Object&gt;();
    dataToSave.put(&quot;StringsArray&quot;, stringGrades); // Save Strings Array
    dataToSave.put(&quot;ObjectsArray&quot;, grades); // Save Objects Array
    mDocRef.set(dataToSave).addOnSuccessListener(new OnSuccessListener&lt;Void&gt;() {
        @Override
        public void onSuccess(Void aVoid) {
            Log.d(TAG, &quot;Document has been saved!&quot;);
            Toast.makeText(getApplicationContext(), &quot;List has been saved successfully!&quot;, Toast.LENGTH_SHORT).show();

Downloading the arrays:

        mDocRef.get().addOnSuccessListener(new OnSuccessListener&lt;DocumentSnapshot&gt;() {
        @Override
        public void onSuccess(DocumentSnapshot documentSnapshot) {
            isListEmpty = false;
            if (documentSnapshot.exists()) {
                Map&lt;String, Object&gt; dataToLoad = (HashMap&lt;String, Object&gt;) documentSnapshot.getData();
                stringGrades = (ArrayList&lt;String&gt;) dataToLoad.get(&quot;StringsArray&quot;);
                grades = (ArrayList&lt;Grade&gt;) dataToLoad.get(&quot;ObjectsArray&quot;);

                for (Grade grade : grades) {    // &lt;------------Fails here
                    System.out.println(grade.toString());
                }

First time I'm trying to use Firebase. It seems that it saves the data in the server successfully. Any advice?

答案1

得分: 2

DocumentSnapshot的get方法返回一个List,其中包含Map

List<Map<String, Object>>

您可以创建另一个类,其中包含您的List<Grade>:

class GradeContainer{
    private List<Grade> grades;

    public GradeContainer(){}

    public List<Grade> getGrades(){
        return grades;
    }
}

然后使用dataToLoad.toObject()

grades = dataToLoad.toObject(GradeContainer.class).getGrades();

您可以阅读以下文档获取更多信息:

https://firebase.google.com/docs/reference/android/com/google/firebase/firestore/DocumentSnapshot?hl=en

以及以下教程:

https://medium.com/firebase-tips-tricks/how-to-map-an-array-of-objects-from-cloud-firestore-to-a-list-of-objects-122e579eae10

英文:

The get method of DocumentSnapshot return a List of Map :

List&lt;Map&lt;String, Object&gt;&gt;

You can create another class with your List<Grade> :

  class GradeContainer{
      private List&lt;Grade&gt; grades;

      public GradeContainer(){}
      
      public List&lt;Grade&gt; getGrades(){
          return grades;
      }
    }

And use dataToLoad.toObject() :

grades = List&lt;Grade&gt; dataToLoad.toObject(GradeContainer.class).getGrades();

You can read this documentation for more informations :

https://firebase.google.com/docs/reference/android/com/google/firebase/firestore/DocumentSnapshot?hl=en

And this tutorial :

https://medium.com/firebase-tips-tricks/how-to-map-an-array-of-objects-from-cloud-firestore-to-a-list-of-objects-122e579eae10

huangapple
  • 本文由 发表于 2020年7月31日 04:15:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/63180726.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定