问题:我正在创建一个允许用户预订办公桌的Android应用,并且遇到了如何从Firebase实时数据库子值中检索数据的问题。
我的目标来自我的代码:我想获取所有用户下所有条目的userId和桌面编号,以便我可以设置已预订的办公桌,以便在我的RecyclerView
上为我的桌面显示特定的图像选择页面。
我当前的代码:我的Firebase数据库是为预订设置的,如下所示:
{
"bookings" : {
"LppMaD3QPBWgMucW8k7mno7tPNu1" : {
"-L9b5LpjYz8yDwXkPUoh" : {
"booked" : true,
"date" : "2018/4/19",
"desk" : "18",
"userId" : "LppMaD3QPBWgMucW8k7mno7tPNu1"
},
"-L9b5iJGeQ_58geFUkZa" : {
"booked" : true,
"date" : "2018/4/13",
"desk" : "14",
"userId" : "LppMaD3QPBWgMucW8k7mno7tPNu1"
},
"-L9b7tcd8kJJQ-q_7UZo" : {
"booked" : true,
"date" : "2018/4/10",
"desk" : "6",
"userId" : "LppMaD3QPBWgMucW8k7mno7tPNu1"
},
"-L9bCktGaluOj7Rw-7_6" : {
"booked" : true,
"date" : "2018/4/20",
"desk" : "6",
"userId" : "LppMaD3QPBWgMucW8k7mno7tPNu1"
}
}
}

与孩子们:userID
,一个独特的推送ID,然后是预订详情。我正在尝试检索唯一推送ID下每个条目的桌面编号和用户ID但是我无法弄清楚如何获取这些值。
我有一个对预订节点的数据库引用。我有一个用于数据库引用的值事件侦听器。在onDataChange
方法中,我添加了一个传递地图的方法和数据快照的值。
我可以看到只有一个键(userId和值都是userId的子项。
我试图创建一个地图并从数据快照中获取桌面值,但我的数组大小只有1,因为它似乎只是检查密钥而不是值。我已经尝试将数据库引用更改为child(" bookings")。child(")尝试引用userId,但它返回相同的内容。
我也尝试获取datasnapshot.child的值("")。getValue();但它的返回方式与datasnapshot.getValue();相同。检查datasnapshot.getChildren();只显示1。
//Connection to the database, gets a reference to the root of the Firebase JSON tree
private DatabaseReference mFirebaseDatabaseReference = FirebaseDatabase.getInstance().getReference();
//Access to the child location
DatabaseReference mDB = mFirebaseDatabaseReference.child("bookings");
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_select_desk);
mDB.addListenerForSingleValueEvent(new ValueEventListener()
{
@Override
public void onDataChange(DataSnapshot dataSnapshot)
{
if (dataSnapshot.getValue() != null)
{
getDeskAndUser((Map<String,Object>) dataSnapshot.getValue());
Log.e(TAG, "Datasnapshot get value: "+dataSnapshot.getValue());
}
else
{
Log.e(TAG, "Datasnapshot is null");
}
}
@Override
public void onCancelled(DatabaseError databaseError)
{
}
}
});
private void getDeskAndUser(Map<String,Object> bookings)
{
ArrayList<String> deskNumbers = new ArrayList<>();
//iterate through each booking, ignoring their UID
for (Map.Entry<String, Object> desk : bookings.entrySet())
{
System.out.println("key=" + desk.getKey() + ", value=" + desk.getValue());
//Get user map
Map singleUserDesk = (Map) desk.getValue();
System.out.println(singleUserDesk);
//Get desk field and append to list
deskNumbers.add((String) singleUserDesk.get("desk"));
}
ArrayList<String> users = new ArrayList<>();
for (Map.Entry<String, Object> userId : bookings.entrySet())
{
System.out.println("key=" + userId.getKey() + ", value=" + userId.getValue());
//Get user map
Map singleUserDesk = (Map) userId.getValue();
//Get desk field and append to list
users.add((String) singleUserDesk.get("userId"));
}
Log.e(TAG, "Array size is " + bookings.size());
// Create an ArrayList of BookingDetails objects
ArrayList<BookingDetails> details = new ArrayList<>();
for (int i = 0; i < bookings.size(); i++)
{
details.add(new BookingDetails());
Log.e(TAG, "Contents of the array list" + details.toString());
}
}
&#13;
我的BookingDetails对象用于在数据库中创建预订
public class BookingDetails {
private static final String TAG = "BookingDetails";
private String userId;
private String key;
private String desk;
private String date;
private boolean booked;
public BookingDetails() {}
public BookingDetails(String userId, String key, String desk, String date, boolean booked) {
this.userId = userId;
this.key = key;
this.desk = desk;
this.date = date;
this.booked = booked;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String getDesk() {
return desk;
}
public String getDate() {
return date;
}
public void setDesk(String desk) {
this.desk = desk;
}
public void setDate(String date) {
this.date = date;
}
public boolean isBooked() {
return booked;
}
public void setBooked(boolean booked) {
this.booked = booked;
}
}
&#13;
我的confirmBooking方法在数据库中设置数据
public void confirm(View view) {
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user != null) {
// User is signed in
String userId = user.getUid();
writeNewBooking(userId, null, mDeskNum, mDateSelected, true);
Intent confirm = new Intent(this, MyBookingsActivity.class);
startActivity(confirm);
} else {
// No user is signed in
Log.e(TAG, "No user is signed in!");
}
}
private void writeNewBooking(String userId, String key, String desk, String date, Boolean booked) {
DatabaseReference postsRef = mFirebaseDatabaseReference.child("bookings").child(userId);
DatabaseReference newPostRef = postsRef.push();
newPostRef.setValue(new BookingDetails(userId, key, desk, date, booked));
newPostRef.getKey();
}
&#13;
答案 0 :(得分:1)
要解决此问题,请使用以下代码:
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference uidRef = rootRef.child("bookings").child(uid);
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String desk = ds.child("desk").getValue(String.class);
String userId = ds.child("userId").getValue(String.class);
Log.d("TAG", desk + " / " + userId);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {}
};
uidRef.addListenerForSingleValueEvent(valueEventListener);
其中相应登录用户的uid
为LppMaD3QPBWgMucW8k7mno7tPNu1
。
输出将是:
18 / LppMaD3QPBWgMucW8k7mno7tPNu1
14 / LppMaD3QPBWgMucW8k7mno7tPNu1
6 / LppMaD3QPBWgMucW8k7mno7tPNu1
6 / LppMaD3QPBWgMucW8k7mno7tPNu1