如何在firebase中存储数据树的数据?用于android studio项目

时间:2018-06-03 03:47:53

标签: android firebase firebase-realtime-database

我想使用Java或Kotlin编程语言为我的android项目数据树存储数据树的数据,如下所示,

"posts": {
  "typeScript": {
    "metrics": {
      "views" : 1200000,
      "likes" : 251000,
      "shares": 1200,
    },
    "title" : "TypeScript Functions",
    "author": "tim",
  },
  "android": {
    "metrics": {
      "views" : 900000,
      "likes" : 117000,
      "shares": 144,
    },
    "title" : "Android app can developed by android studio or flutter",
    "author": "scott",
  }
}
pz,帮帮我吧。

2 个答案:

答案 0 :(得分:0)

Firebase中有一个名为Object的字段类型。您可以将其用于指标字段。

Try this structure

答案 1 :(得分:0)

您能告诉我们您的结构数据是哪个?类〜? 打字稿和android是2类还是只有1?

private final  DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference();

private DatabaseReference typeListReference = databaseReference.child("typeScript");

private DatabaseReference androidListReference = 
    databaseReference.child("android");   

private void writeNewAndroid(String views, String likes, String share, String title, String others)
        {
            try {
                Android android = new Android (views, , likes, share, title, others);
                androidListReference.child("metrics").setValue(android );
            }catch (Exception e)
            {
                e.printStackTrace();
            }
        }