简体   繁体   中英

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

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: 在此处输入图像描述

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.

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.

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

This doesn't work (and I'm surprised it even compiles):

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

To write back the incremented number of sales:

snapshot.getReference().setValue(snapshot.getValue(Long.class)+1);

Note that in general using such sequential numeric keys in Firebase is an anti-pattern. I highly recommend using Firebase's native push() keys, which are also always incrementing but provide much stronger guarantees outside of that. To read more about that, check out: Best Practices: Arrays in Firebase .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM