使用FirebaseRecyclerOption <>时如何在Firebase数据库中检索子数据的子级?

时间:2019-02-07 04:56:43

标签: android json firebase firebase-realtime-database

在Firebase中检索孩子的多个数据的孩子时,没有使用FirebaseRecyclerOption <>的想法。如图所示,我已经检索了单个孩子的数据,但是很难与FirebaseRecyclerOption <>

一起检索孩子的数据(学生)的孩子。
{
  "message" : "Hii!",
  "posts" : {
    "0" : {
      "desc" : "cse symposium",
      "id" : 0,
      "students" : [ "john", "vini", "honey", "ee", "larry", "jack" ],
      "title" : "addict2K18",
      "url" : "http://knowafest.com/files/uploads/Brochurefinal-2018082907.png"
    },
    "1" : {
      "desc" : "ece symposium",
      "id" : 1,
      "students" : [ "shalini" ],
      "title" : "8th National Level Technical Symposium ZENFOX2K19",
      "url" : "https://www.pacolleges.org/images/demo/ece/final%20zenfox.jpg"
    },
    "-LWFBGrrTE_miEVdlCcw" : {
      "desc" : "every year march 14",
      "id" : "-LWFBGrrTE_miEVdlCcw",
      "students" : [ "Udayasankar", "kumaran", "mitch", "jack" ],
      "title" : "panchamithara",
      "url" : "http://knowafest.com/files/uploads/panchamithra17-pollachi-cultural-fest.jpg"
    }
  }
}





private void fetch() {
        final Query query = FirebaseDatabase.getInstance()
                .getReference()
                .child("posts");
//          FirebaseDatabase.getInstance().setPersistenceEnabled(true );  
        FirebaseRecyclerOptions<Model> options =
                new FirebaseRecyclerOptions.Builder<Model>()
                        .setQuery(query, new SnapshotParser<Model>() {
                            @NonNull
                            @Override
                            public Model parseSnapshot(@NonNull DataSnapshot snapshot) {
                                return new Model(snapshot.child("id").getValue().toString(),
                                        snapshot.child("title").getValue().toString(),
                                        snapshot.child("desc").getValue().toString(),
                                        snapshot.child("url").getValue().toString());
                            }
                        })
                        .build();



        adapter = new FirebaseRecyclerAdapter<Model, ViewHolder>(options) {
            @Override
            public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                View view = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.list_item, parent, false);

                return new ViewHolder(view);
            }

0 个答案:

没有答案