Ad

Saving Many Child Under One Parent In Firebase Android

I have tried the below code in the onDataChange() method to save the item click in the firebase storage and I am trying to add the item into cart under the single user that can have many items(as child) in its cart. But the problem with this is that the push() generates the new id under the parent id and going with the infinite loop and saving the item infinite times when on Item click.And without the push() the item in the cart is replaced with the new one. And I want to add the items as child under the same user who is logged in. I am new to firebase and android so please help me out here. I have also provided the screenshot of the data which is saving in firebase, when I click on one item it saves the item infinite times into firebase.

 private void getDetailedItem(final String itemId)
{
    items.child(itemId);
    items.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot)
        {

            for(DataSnapshot datas: dataSnapshot.getChildren())
            {         

                if(pos.equals(datas.getKey().toString()))
                {
                    try {
                        item = dataSnapshot.getValue(Items.class);

                        cart_item_name = datas.child("title").getValue().toString();
                        cart_item_amount = datas.child("amount").getValue().toString();
                        image_view_cart = datas.child("image").getValue().toString();

                        Toast.makeText(CartActivity.this, "Data setting",
                                Toast.LENGTH_SHORT).show();
                        item.setTitle(cart_item_name);
                        item.setAmount(cart_item_amount);
                        item.setImage(image_view_cart);

                        Items uploadUser = new Items();
                        uploadUser.setTitle(cart_item_name);
                        uploadUser.setAmount(cart_item_amount);
                        uploadUser.setImage(image_view_cart);

                        if(mFirebaseAuth.getCurrentUser()!=null){
                           items.child("UserNode").child(mFirebaseAuth.getCurrentUser().getUid()).setValue(
                                            item, new DatabaseReference.CompletionListener() {
                                        @Override
                                        public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
                                            if (databaseError == null) {

                                                Toast.makeText(CartActivity.this, "Data is saved successfully",
                                                        Toast.LENGTH_SHORT).show();                                                    
                                            }
                                        }
                                    });
                        addToCartItems.add(item);
                        cartRecyclerAdapter.notifyDataSetChanged();
                    }
                    catch (Exception e)
                    {
                        e.printStackTrace();
                    }
                }
            }
            updateAdapterData();
        }

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

Click to see the ScreenShot

Ad

Answer

Your question is not clear but as far as I understood it, you are trying to make a shopping cart where logged in user can add and remove items and keep it stored in firebase database for future use.

Firstly, if you want to fetch the details of particular item then use addListenerForSingleValueEvent.

Secondly, call notify adapter after you get completion callback. So call cartRecyclerAdapter.notifyDataSetChanged(); inside onComplete().

private void getDetailedItem(final String itemId)
{
items.child(itemId);
items. addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot)
    {

        for(DataSnapshot datas: dataSnapshot.getChildren())
        {         

            if(pos.equals(datas.getKey().toString()))
            {
                try {
                    item = dataSnapshot.getValue(Items.class);

                    cart_item_name = datas.child("title").getValue().toString();
                    cart_item_amount = datas.child("amount").getValue().toString();
                    image_view_cart = datas.child("image").getValue().toString();

                    Toast.makeText(CartActivity.this, "Data setting",
                            Toast.LENGTH_SHORT).show();
                    item.setTitle(cart_item_name);
                    item.setAmount(cart_item_amount);
                    item.setImage(image_view_cart);

                    Items uploadUser = new Items();
                    uploadUser.setTitle(cart_item_name);
                    uploadUser.setAmount(cart_item_amount);
                    uploadUser.setImage(image_view_cart);

                    if(mFirebaseAuth.getCurrentUser()!=null){
                       items.child("UserNode").child(mFirebaseAuth.getCurrentUser().getUid()).setValue(
                                        item, new DatabaseReference.CompletionListener() {
                                    @Override
                                    public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
                                        if (databaseError == null) {
                                            addToCartItems.add(item);
                                    cartRecyclerAdapter.notifyDataSetChanged();

                                            Toast.makeText(CartActivity.this, "Data is saved successfully",
                                                    Toast.LENGTH_SHORT).show();                                                    
                                        }
                                    }
                                });

                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }
            }
        }
        updateAdapterData();
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {}
});
}
Ad
source: stackoverflow.com
Ad