首页 > 解决方案 > 如何遍历数据库以检索评论部分

问题描述

我正在尝试遍历 firebase 以检索每个用户提交的所有评论,并且还添加了他们的用户名,以便我可以匹配它们,因为每个评论对用户来说都是独一无二的。我只是发现附加他们的用户名而不是使用我的图片中显示的增量值更容易......我可以让数组显示在屏幕上但是当使用数组检索 info.getReview() 方法时,我变得空...我猜,第二个数组有两个用户名钢琴和音乐家。应该只说音乐家正确吗?我不确定我是否在这里正确执行此操作,但我认为我应该使用数组来循环...

DatabaseReference reference = FirebaseDatabase.getInstance().getReference().child("Medical Clinics"); // getReference() is the root
        reference.addValueEventListener(new ValueEventListener() {
            @RequiresApi(api = Build.VERSION_CODES.N)
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                list.clear();
                for (DataSnapshot snapshot : dataSnapshot.getChildren()) {


                    int count = (int) snapshot.child("ID").getChildrenCount();



                    Information info = snapshot.getValue(Information.class);


                   for (i = 0; i < count; i++) {

                       String Id = String.valueOf(snapshot.child("ID").getValue());

                       Id = Id.replace("{", "");
                       Id = Id.replace("}", "");
                       Id = Id.replaceAll("Id by", "");
                          String[] username = Id.split("=");
                //       Id = Id.substring(Id.indexOf("=") + 1);


                        Information info2 = snapshot.child("Reviews, Rating and Services by " + username[i]).getValue(Information.class);


                        //       for (int i =0; i < words.length; i++) {
                        assert info != null;
                   //     assert info2 != null;

                        String txt;

                     //   if (info2.getReview().isEmpty()) {
                    //        Toast.makeText(View_Review.this, "There are no Reviews provided yet for this particular Medical Clinic!", Toast.LENGTH_SHORT).show();
                    //        return;
                     //   } else {
                     //       txt = "Medical Clinic: " + info.getName() + "\n\nReviews: " + info2.getReview(); //+ "\n\nService 1: " + info2.getServices() + "\n\nService 2: " + info2.getServices2() + "\n\nService 3: " + info2.getServices3() + "\n\nService 4: " + info2.getServices4() + "\n\nService 5: " + info2.getServices5() + "\n\nService 6: " + info2.getServices6() + "\n\nService 7: " + info2.getServices7() + "\n\nService 8: " + info2.getServices8() + "\n\nRating: " + info2.getRating();
                            txt = "ID: " + username[i];

                             System.out.println(Id);
                         //    System.out.println(info2.getReview());

                            list.add(txt);
                        }
                adapter.notifyDataSetChanged();

                    }
              //  }
            }

在此处输入图像描述

标签: javafirebase-realtime-database

解决方案


似乎您的结构可以简化如下:

-RingWood Family Medical Center
 |
  -MJ0jfdsk2(unique id for user)
   |
    -rating
    -review
    -services3
    -username

  -MJ1kjksf2(unique id for user 2)
   |
    -rating
    -review
    -services3
    -username

    ...

通过执行上述操作,您可以轻松地遍历 RingWood 家庭医疗中心的孩子并获得用户名和评论。

有关 JSON 结构最佳实践的更多信息,您可以从 Firebase 页面阅读以下内容。参考:https ://firebase.google.com/docs/database/web/structure-data

快乐编码!


推荐阅读