1

I have two tables - cartOrders and placedOrders. cartOrders is used to add products to cart, and placedOrders is activated when the user presses "Place Order" button. I need to copy data from cartOrders to placedOrders when the user clicks the "Place Order" button, but I'm not able to do so.

cartDatabase = FirebaseDatabase.getInstance().getReference("cartOrders");
cartQuery = cartDatabase.child(orderID).orderByChild("productQuantity").startAt(1);
placedOrdersDatabase = FirebaseDatabase.getInstance().getReference("placedOrders");
placedOrderQuery = placedOrdersDatabase.child("orderID");
placeOrderButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        cartDatabase.child(orderID).addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                //Logic to add data to data to placedOrders.
                String name = dataSnapshot.child("productName").getValue(String.class);
                System.out.println("ProductName: " + name);
            }

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

            }
        });

        Intent intent = new Intent(getApplicationContext(), OrderPlacedActivity.class);
        intent.putExtra("ORDER_ID", orderID);
        startActivity(intent);
    }
});

In the code, I was checking if the code is picking up the data from the database, but it wasn't. The output is null. Both the tables have the same structure. Any help will be appreciated. Thanks!

My cartOrders table structure:

cartOrders:
----------OrderID:
-----------------ProductID:
---------------------------ProductID:
---------------------------ProductName:
---------------------------ProductPrice:
---------------------------ProductQty:
Lutzi
  • 416
  • 2
  • 13
mayur
  • 374
  • 1
  • 10

1 Answers1

0

@Alex Mamo answered this question on a different question.

private void copyRecord(Query fromPath, final DatabaseReference toPath) {

        ValueEventListener valueEventListener = new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                toPath.setValue(dataSnapshot.getValue()).addOnCompleteListener(new OnCompleteListener<Void>() {
                    @Override
                    public void onComplete(@NonNull Task<Void> task) {

                        if (task.isComplete()) {

                            System.out.println("Complete");

                        } else {

                            System.out.println("Failed");

                        }

                    }
                });

            }

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

            }
        };

        fromPath.addListenerForSingleValueEvent(valueEventListener);

    }

The code works fine. Link:Moving or copying data from one node to another in firebase database

mayur
  • 374
  • 1
  • 10