1

I am trying to develop an android app where I need to send the data to Firebase DB having it's schema as mentioned below:

 { 
   "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"
                           }                     
                     ]
                  }

              ]
       }
 }

I have a BookDetailActivity, in which I'm showing the chapters in RecyclerView. So, I can get the "unique_id" & "Book_id" from the BookDetailActivity. After clicking on the Chapters' recyclerview, I need to send the values as specified in the above schema.

Also, I need to retrieve those values in activity.

The java code mentioned for the Chapters' recyclerview, present in the BookDetailActivity is mentioned below:

  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);
                }
            });

        }
    };

Help me in achieving the same.

Vipul K. Patil
  • 128
  • 1
  • 10
  • To update data in multiple locations with one request, see https://firebase.googleblog.com/2015/09/introducing-multi-location-updates-and_86.html, https://firebase.googleblog.com/2015/10/client-side-fan-out-for-data-consistency_73.html and https://stackoverflow.com/questions/41237339/change-values-in-firebase-database-nodes-by-a-given-criteria/41246031#41246031 – Frank van Puffelen Sep 10 '17 at 14:31
  • Can we fetch the values once I have added those values? If yes, please tell me how to do it.. – Vipul K. Patil Sep 14 '17 at 15:30

0 Answers0