我正在尝试在我的应用程序中实现一项功能,以便如果用户选择一家餐厅,则会显示该餐厅中可用商品的列表,如果没有,则显示一个文本和一个按钮。
我这样处理:我将比较两个子项目,餐厅名称以检查它们是否相等,如果相等,它将显示一个recyclerview以及可用项目列表。
Here's a screenshot of my firebase database
这是我如何保存到FoodRequestRest数据库子级的代码段
requestPlceTxt = placeTxt.getText().toString();
System.out.println("First instance of Request place her : " + requestPlceTxt);
FirebaseUser user = mAuth.getCurrentUser();
userID = user.getUid();
// String id = UserReqPlaceRef.push().getKey();
Request request = new Request(requestPlceTxt);
// save to database
UserReqPlaceRef.child("Food Request Restaurant")
.child(userID)
.push()
.setValue(request);
这是我如何检索数据库并将其显示在回收者视图中的代码段
// Initialize FirebaseAuth
mFirebaseAuth = FirebaseAuth.getInstance();
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
final RecyclerView recyclerView = (RecyclerView) findViewById(R.id.dummyfrag_scrollableview);
noItemsTxt = (TextView) findViewById(R.id.no_items);
OkExitBtn = (Button) findViewById(R.id.ok_exit);
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setHasFixedSize(true);
linearLayoutManager.setReverseLayout(true);
linearLayoutManager.setStackFromEnd(true);
final List<Donation> donationList = new ArrayList<Donation>();
final DonationAdapter adapter = new DonationAdapter(donationList, this);
recyclerView.setAdapter(adapter);
noItemsTxt.setVisibility(View.GONE);
OkExitBtn.setVisibility(View.GONE);
final String curtUserId = FirebaseAuth.getInstance().getCurrentUser().getUid();
final FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference rootRef = database.getReference();
// get key for each restuarant chosen to make a donation
final String donateId = rootRef.child("All Donations").getKey();
// get key each restuarant chosen to make a request
final String requestId = rootRef.child("Food Request Restaurant").child(curtUserId).getKey();
final Query latestQuery = rootRef
.child("All Donations")
// .child(curtUserId)
.orderByKey()
.limitToFirst(50);
final DatabaseReference reqRestLoc = rootRef
.child("Food Request Restaurant")
.child(curtUserId)
.child(requestId);
// code here to retrieve the string value located at reqRestLoc
// then compare it with donateRest
// make another value event listener and get the string?
latestQuery.addValueEventListener(new com.google.firebase.database.ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
donationList.clear();
for (DataSnapshot gigSnapshot : dataSnapshot.getChildren()) {
Donation donation = gigSnapshot
.getValue(Donation.class);
donationList.add(donation);
adapter.notifyDataSetChanged();
String donateRest = (String) dataSnapshot
.child("All Donaations")
.child(donateId)
.getValue();
Log.d(donateRest, "This is the ");
if (s.equals(donateRest)) { // s is the string value gotten from the restReqLoc
recyclerView.setVisibility(View.VISIBLE);
noItemsTxt.setVisibility(View.GONE);
OkExitBtn.setVisibility(View.GONE);
OkExitBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DisplayItems.super.onBackPressed();
}
});
}
else {
recyclerView.setVisibility(View.GONE);
noItemsTxt.setVisibility(View.VISIBLE);
OkExitBtn.setVisibility(View.VISIBLE);
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
我尝试了比较这两个字符串的不同方法,但是我无法同时访问这两个数据项,这要么是我引用了All Donations的孩子,要么是FoodRequestRestuarant的孩子。有办法吗?