gpt4 book ai didi

java - Firebase 数据库使用 map 获取子项的特定值

转载 作者:行者123 更新时间:2023-11-30 10:17:28 24 4
gpt4 key购买 nike

问题:我正在创建一个允许用户预订服务台的 Android 应用程序,但遇到了如何从 Firebase 实时数据库子值中检索数据的问题。

我的代码目标:我想获取所有用户下所有条目的 userId 和办公 table 号,这样我就可以将已预订的办公 table 设置为在我的 RecyclerView 上显示特定图像用于我的办公 table 选择页面。

我当前的代码:我的 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"
}
}
}

对于 child :userID,唯一的推送 ID,然后是预订详情。我正在尝试检索唯一推送 ID 下每个条目的办公 table 号码和用户 ID,但我不知道如何获取这些值。

我有一个对预订节点的数据库引用。我有一个用于数据库引用的单值事件监听器。在 onDataChange 方法中,我添加了一个传入 map 和数据快照值的方法。

我可以看到只有一个key(userId和values都是userId的children。

我试图创建一个 map 并从数据快照中获取桌面值,但我的数组大小仅为 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());

}
}

我的 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;
}
}

还有我的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();
}

最佳答案

要解决这个问题,请使用以下代码:

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);

其中对应登录用户的uidLppMaD3QPBWgMucW8k7mno7tPNu1

输出将是:

18 / LppMaD3QPBWgMucW8k7mno7tPNu1
14 / LppMaD3QPBWgMucW8k7mno7tPNu1
6 / LppMaD3QPBWgMucW8k7mno7tPNu1
6 / LppMaD3QPBWgMucW8k7mno7tPNu1

关于java - Firebase 数据库使用 map 获取子项的特定值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49724137/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com