Firebase将数据另存为List,有时另存为Map对象 [英] Firebase saves data as List and sometimes as Map object

查看:83
本文介绍了Firebase将数据另存为List,有时另存为Map对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用键将数据保存为数字值,其中键是用户尝试的问题.如果用户尝试了所有问题,例如,它可能是连续的.键可能是0、1、2、3 ...由Firebase保存为Array(如图所示)

I am saving data with key as numeric values, where keys are questions attempted by user. It may be continuous if user has attempted all the questions eg. keys may be 0,1,2,3... this is saved by Firebase as Array (as in image)

JSON是

"parthgupta48@gmail,com": {
  "attemptedQuestions": [
    {
      "answer": "533",
      "status": "wrong",
      "submissionTime": 1487796702453
    },
    {
      "answer": "9",
      "score": 10,
      "status": "correct",
      "submissionTime": 1487878744867
    },
    {
      "answer": "4",
      "status": "wrong",
      "submissionTime": 1487956858227
    },
    {
      "answer": "3",
      "status": "wrong",
      "submissionTime": 1488056247303
    },
    null,
    {
      "answer": "25",
      "score": 16,
      "status": "correct",
      "submissionTime": 1488212417465
    },
    {
      "answer": "3",
      "status": "wrong",
      "submissionTime": 1488380348815
    },
    null,
    null,
    null,
    {
      "answer": "50",
      "score": 11,
      "status": "correct",
      "submissionTime": 1488648738615
    }
  ],

当键不连续时,例如0、9、15 ...当用户仅尝试某些问题时,就会发生这种情况,然后Firebase将其另存为地图(如图片所示)

When keys are discontinuous eg 0,9,15... this happen when when user has attempted only certain questions then Firebase saves this as map (as in image)

JSON是

"priyanshu96,goyal@gmail,com" : {
      "attemptedQuestions" : {
        "0" : {
          "answer" : "667",
          "status" : "wrong",
          "submissionTime" : 1487773682189
        },
        "10" : {
          "answer" : "50",
          "submissionTime" : 1488646361162
        }
      },

这是我用来在Android中检索数据的代码.

This is my code I used to retrieve data in Android.

try {
       mCurrentUser = child.getValue(UserObject.class);
    } catch (Exception e) {

       Utils.makeToast("Some Error Occurred ",LoginActivity.this);

   }

userObject类

public class UserObject implements Serializable {

private String name;
private String uid;
private String email;
private String photoUrl;
private String contact;//bekaar
private long credits;
public Map<String,AttemptedByUserObject> attemptedQuestions;

public void setAttemptedQuestions(Map<String, AttemptedByUserObject> attemptedQuestions) {
    this.attemptedQuestions = attemptedQuestions;
}

public Map<String, AttemptedByUserObject> getAttemptedQuestions() {
    return attemptedQuestions;
}

public long getCredits() {
    return credits;
}

public void setCredits(long credits) {
    this.credits = credits;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getUid() {
    return uid;
}

public void setUid(String uid) {
    this.uid = uid;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public String getPhotoUrl() {
    return photoUrl;
}

public void setPhotoUrl(String photoUrl) {
    this.photoUrl = photoUrl;
}

public String getContact() {
    return contact;
}

public void setContact(String contact) {
    this.contact = contact;
}}

但是问题是,将数据另存为列表时,必须将attemptedQuestions列为列表,而将数据另存为Map时,必须将其为Map.

But problem is attemptedQuestions needs to be list when data is saves as list and it need to be Map when data is saved as Map.

我该如何解决这个问题?

How can I solve this problem?

推荐答案

使用从零开始(例如0、1、2等)的连续键保存信息时,您将返回一个列表,在我的情况下,是从One开始的,因此由于某种原因,它使我返回了一个以null元素开头的列表.当键不连续时,将返回一个Map.了解了这一点之后,我最终通过类型检查对其进行了修补,由于列表中的空值,我删除了第一个元素.

When the information is saved with consecutive keys starting from Zero (I.e. 0, 1, 2, etc.) you will get back a list, in my case I was starting from One so for some reason it got me back a list starting with a null element. When keys are discontinuous a Map will be returned. After understanding this I ended up patching it with a type check, and because of the null in the list cases I remove the first element.

when (it) {
    is Map<*, *> -> doSomething(it.values.toList() as List<HashMap<String, Any>>)
    is List<*> -> doSomething((it.subList(1,it.size)) as List<HashMap<String, Any>>)
    is Any -> return
}

在示例中,doSomething仅接受List<Hashmap<String, Any>>自变量,但变量it可以是Map或List.

In the example doSomething only takes List<Hashmap<String, Any>> arguments, but the variable it can be either a Map or a List.

这篇关于Firebase将数据另存为List,有时另存为Map对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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