如何在Firebase中同时在2个不同的孩子中读写数据

时间:2017-09-10 10:34:53

标签: java android firebase firebase-realtime-database

我正在尝试开发一个Android应用,我需要将数据发送到具有以下所述的架构的Firebase数据库:

 { 
   "Usercart": { 
       "unique_id": {
             "Book": [
                  "Book_id1" : {
                     "book_title": "abc",
                     "book_url": "imageUrl",
                     "chapters_purchased": [ // multiple chapters
                           "chapter_3": {
                               "chapter_title":"title3",
                               "chapter_page_count":"16"
                           },
                           "chapter_4": {
                               "chapter_title":"title4",
                               "chapter_page_count":"18"
                           }                     
                     ]
                  },

                  "Book_id2" : {
                     "book_title": "pqr",
                     "book_url": "imageUrl2",
                     "chapters_purchased": [ // multiple chapters
                           "chapter_5": {
                               "chapter_title":"title3",
                               "chapter_page_count":"16"
                           },
                           "chapter_9": {
                               "chapter_title":"title4",
                               "chapter_page_count":"18"
                           }                     
                     ]
                  }

              ]
       }
 }

我有一个BookDetailActivity,我在其中展示了RecyclerView中的章节。所以,我可以得到" unique_id" &安培; " Book_id"来自BookDetailActivity。点击章节后#39; recyclerview,我需要发送上述模式中指定的值。

另外,我需要在活动中检索这些值。

为章节提到的java代码'在BookDetailActivity中出现的recyclerview如下所述:

  FirebaseRecyclerAdapter<Chapters, ChapterHolder> chapter_name_adapter = new FirebaseRecyclerAdapter<Chapters, ChapterHolder>(
            Chapters.class,
            R.layout.simple_chapters_horizontal_list,
            ChapterHolder.class,
            refForBookName.child("Chapter")) {
        @Override
        protected void populateViewHolder(ChapterHolder viewHolder, final Chapters model, final int position) {
            viewHolder.setChapterName(model.getTitle());
            viewHolder.buy_or_view_botton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    DatabaseReference refForCart = FirebaseDatabase.getInstance().getReference().child("UserCart").child(uniqueId + "").child("Book");

                    Calendar c = Calendar.getInstance();
                    System.out.println("Current time => " + c.getTime());
                    SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");

                    String formattedDate = df.format(c.getTime());
                    BookCartOfChapters bookCartOfChapters = new BookCartOfChapters();
                    bookCartOfChapters.setBookUrl(bookImageUrl);
                    bookCartOfChapters.setBookTitle(bookTitle);
                    Chapters_purchased chapters = new Chapters_purchased();
                    chapters.setChapter_title(model.getTitle());
                    chapters.setDatetime(formattedDate);
                    chapters.setPage_count(model.getPage_count());
                    chapters.setPrice(model.getPrice());
                    chapters_purchased.add(chapters);
                    bookCartOfChapters.setChapters_purchased(chapters_purchased);

                    //refForCart.child(bookId+"").setValue(book);
                    refForCart.child(bookId + "").setValue(bookCartOfChapters);
                }
            });

            viewHolder.chaptersLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent toChaptersActivity = new Intent(BookDetailActivity.this, FolioActivity.class);
                    toChaptersActivity.putExtra(FolioActivity.INTENT_EPUB_SOURCE_TYPE, FolioActivity.EpubSourceType.ASSETS);
                    toChaptersActivity.putExtra(FolioActivity.INTENT_EPUB_SOURCE_PATH, "epub/One Amazing Thing - Chitra Banerjee Divakaruni.mobi");
                    startActivity(toChaptersActivity);
                }
            });

        }
    };

帮助我实现同样的目标。

0 个答案:

没有答案