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) {}
});
}
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) {}
});
}
Related Questions
- → How to update data attribute on Ajax complete
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → Octobercms Component Unique id (Twig & Javascript)
- → Passing a JS var from AJAX response to Twig
- → Laravel {!! Form::open() !!} doesn't work within AngularJS
- → DropzoneJS & Laravel - Output form validation errors
- → Import statement and Babel
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM