[英]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.我在一个节点下保存了一些数据,该节点具有当前登录的用户 UID,以及发布时间戳。我成功地设法检索了该节点下的所有数据,但我只想检索当前登录用户的数据,我我正在尝试创建类似于亚马逊在购物车中存储商品的方式,当用户单击购物车时,它会显示他们已添加到那里的商品,而不是每个用户的所有商品。 Can someone assist me with this problem?有人可以帮我解决这个问题吗?
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);
}
});
}
First of all, you need to change your DB json to this one.首先,您需要将您的 DB json 更改为这个。
Cart -userUid -cartUid -datee -description -andSoOn购物车 -userUid -cartUid -datee -description -andSoOn
That's mean, when you are storing the item into Cart
.这意味着,当您将商品存储到Cart
中时。 You need to include the userUid
.您需要包括userUid
。
//Getting userUid final FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser(); //获取 userUid final FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser(); if (firebaseUser.= null) { final String userUid = firebaseUser;getUid().如果 (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(); } }); uploadPostRef.child("Cart").child(userUid).child(timeStamp).setValue(userDictionary).addOnSuccessListener(new OnSuccessListener() { @Override public void onSuccess(Void unused) { Log,i("添加成功"。 “成功添加到购物车。;”); getLoggedInUserData(); } }); } }
To retrieve the Cart
according to the user.根据用户检索Cart
。 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()) { public void downloadCartData(){ cartDb.child("Cart").child(userUid).addListenerForSingleValueEvent(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { for (DataSnapshot data: dataSnapshot.getChildren()) { 如果(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); } });
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.