Skip to content
Advertisement

How do I retrieve data under UID for the signed in user with Firebase

I saved some data under a node with the currently signed in user UID, along with the post timestamp.I successfully manage to retrieve all the data under this node, but I would only like to retrieve the data for the currently signed in user, I am trying to create something similar to how amazon stores an item in the cart and when the user clicks the cart it shows the items they have added there, not all the items for every user. Can someone assist me with this problem?

My database

public void addItemToCart(){

    SimpleDateFormat formatter = new SimpleDateFormat(" HH:mm:ss ");
    Date date = new Date(System.currentTimeMillis());
    timeStamp = loggedInUserId + formatter.format(date);


    userDictionary.put("itemPrice", selectedPrice);
    userDictionary.put("productname",productNameText);
    userDictionary.put("description", selectedStringProductDescrtipon);
    userDictionary.put("uid",loggedInUserId);
    userDictionary.put("productImage", selectedImage);
    userDictionary.put("datee",date.toString());
    userDictionary.put("name",loggedInUserName);
    userDictionary.put("timestamp",timeStamp);




    uploadPostRef.child("Cart").child(timeStamp).setValue(userDictionary).addOnSuccessListener(new OnSuccessListener<Void>() {
        @Override
        public void onSuccess(Void unused) {
            Log.i("sucessfully added","sucesssfully added to cart..");
            getLoggedInUserData();

        }
    });
    numberInCartIv.setVisibility(View.VISIBLE);

    public void downloadCartData(){
        cartDb.child("Cart").addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                for (DataSnapshot data : dataSnapshot.getChildren()) {
                    if (data.exists()) {

                        Log.i("data",data.toString());

//                        cartModel = data.getValue(CartModel.class);
//                        cartModelArrayList.add(cartModel);
//                        LinearLayoutManager horizontalLayoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
//                        cartRecyleView.setLayoutManager(horizontalLayoutManager);
//                        cartAdapter  = new CartAdapter(cartModelArrayList, getContext());
//                        cartRecyleView.setAdapter(cartAdapter);


                    } else {
                        Log.i("error", "Error Loading JSON data..");

                    }

                }


            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                String error = databaseError.getDetails();
                Log.i("error", error);

            }
        });

    }

Advertisement

Answer

  1. First of all, you need to change your DB json to this one.

    Cart -userUid -cartUid -datee -description -andSoOn

  2. That’s mean, when you are storing the item into Cart. You need to include the userUid.

    //Getting userUid final FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser(); if (firebaseUser != null) { final String userUid = firebaseUser.getUid(); uploadPostRef.child(“Cart”).child(userUid).child(timeStamp).setValue(userDictionary).addOnSuccessListener(new OnSuccessListener() { @Override public void onSuccess(Void unused) { Log.i(“sucessfully added”,”sucesssfully added to cart..”); getLoggedInUserData(); } }); }

  3. To retrieve the Cart according to the user. You just call them like this.

    public void downloadCartData(){ cartDb.child(“Cart”).child(userUid).addListenerForSingleValueEvent(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { for (DataSnapshot data : dataSnapshot.getChildren()) { if (data.exists()) {

                         Log.i("data",data.toString());
    
                     } else {
                         Log.i("error", "Error Loading JSON data..");
    
                     }
                 }
             }
    
             @Override
             public void onCancelled(DatabaseError databaseError) {
                 String error = databaseError.getDetails();
                 Log.i("error", error);
    
             }
         });
    

    }

Advertisement