Home > Net >  How can I write Arraylist data to Firebase Database in Java?
How can I write Arraylist data to Firebase Database in Java?

Time:11-23

I have an android app with firebase as the backend. I have an activity where the use populates a list for a recycler view which they want to save to the database.

Below is a screenshot of the Firebase Realtime Database structure of my database: enter image description here

The values of children in the Values node is what I'm using to create the id for new data that is added.

I have an arraylist of sales objects populated by the user which is to be saved in a new Sales node and would like to use the value in Values/Sales to populate the ids for each item in the arraylist.

Below is my code for saving that data. However in the database, only the last item in the arraylist is saved.

ArrayList<Inventory> salesArrayList = new ArrayList<>();

------------------- Code for populating data into the arraylist ----------------------

for (int i = 0; i < salesArrayList.size(); i  ){
                        int finalI = i;
                        databaseReference.child("Values").child("Sales").addListenerForSingleValueEvent(new ValueEventListener() {
                            @Override
                            public void onDataChange(@NonNull DataSnapshot snapshot) {
                                String newCount = String.valueOf(Integer.parseInt(String.valueOf(snapshot.getValue())) 1); // Gets the original value saved and adds 1 to it to be used as the id in the 'Sales' node
                                DatabaseReference countReference = databaseReference.child("Sales").child(newCount);
                                countReference.child("date").setValue(currentDate);
                                countReference.child("name").setValue(salesArrayList.get(finalI).getName());
                                countReference.child("quantity").setValue(salesArrayList.get(finalI).getQuantity());
                                countReference.child("unit_price").setValue(salesArrayList.get(finalI).getValue()/salesArrayList.get(finalI).getQuantity());
                                countReference.child("value").setValue(salesArrayList.get(finalI).getValue());

                                databaseReference.child("Values").child("Sales")(Integer.parseInt(newCount)); // Updates the number of sales
                            }

                            @Override
                            public void onCancelled(@NonNull DatabaseError error) {

                            }
                        });
                    }

If there are 7 items in the arraylist, I'd like a way to have all them saved in the Sales similar to the Purchases node rather than only the last item on the arraylist.

Thank you in advance.

CodePudding user response:

I can't comment, but I can answer, but I don't know if my answer will be correct.

If you are only updating the last item on the array it would seem to me that you are at each iteration overwriting the same object and finally saving only that object to the database. Is there an append method? Or does the documentation say anything about this? This for me seems the most probable cause.

Maybe you each time need a new instance of the 'databaseReference'-object.

CodePudding user response:

I'm not able to comment. Can you please paste the screenshot of the tree after the last element is added?

  • Related