如何比较两个不同的Firebase数据库子项f

时间:2018-08-05 21:37:44

标签: android firebase firebase-realtime-database

我需要帮助才能在我的应用中实现功能,这样,如果用户选择了一家餐厅,则会显示该餐厅中可用商品的列表,如果没有,则会显示一个文本和一个按钮。

我这样做是这样的:我将比较两个子项目,餐厅名称以检查它们是否相等。如果相等,它将显示一个recyclerview并列出可用项目。

Here is a screenshot of my firebase database structure

我的目标是 1.比较donationplceTxt(“所有捐赠”子项下)的字符串是否与所在位置(“ Food Request Restaurant”下的字符串)相等。

这是我从哪里获得餐厅名称的代码段。请注意,它在另一个名为RequestActivity.java的活动中。当我想在DisplayItems.java内的回收站视图中显示数据库中的项目时,便完成了实际比较。

用于处理该位置的RequestActivity.java的代码段

 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == PLACE_PICKER_REQUEST) {
        if (resultCode == RESULT_OK) {

            // check to see if selected place is a restaurant
            boolean isARestaurant = false;
            Place place = PlacePicker.getPlace(data, this);
            for (int i : place.getPlaceTypes()) {
                if (    i == Place.TYPE_RESTAURANT ||
                        i == Place.TYPE_CAFE
                        ) {
                    isARestaurant = true;
                    StringBuilder stBuilder = new StringBuilder();
                    String placename = String.format("%s", place.getName());
                    // String latitude = String.valueOf(place.getLatLng().latitude);
                    // String longitude = String.valueOf(place.getLatLng().longitude);
                    String address = String.format("%s", place.getAddress());
                    stBuilder.append(placename);
                    stBuilder.append(", ");
                    stBuilder.append(address);
                    placeTxt.setText(stBuilder.toString());
                    mkRqstBtn.setVisibility(View.VISIBLE);

                    requestPlceTxt = placeTxt.getText().toString();
                    System.out.println("First instance of Request place here : " + 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);

                    // use intent extra to send data to display items activity
                    // Intent myintent=new Intent(RequestActivity.this, DisplayItems.class).putExtra("sentPlace", requestPlceTxt);
                    // startActivity(myintent);

                    break;
                }
                if(isARestaurant){
                    //Right type of place
                }else{
                    //Tell to the user to select an appropriate place = restaurant
                    Toast.makeText(this, "Please select a Restaurant",Toast.LENGTH_LONG).show();
                }
            }
        }
    }

DisplayActivity.java中的代码段,我应该在其中显示该特定餐厅的可用项目(如果有)

// get key id for each instance of donation
    final String donateId =   rootRef.child("All Donations").child(curtUserId).getKey();
    // get key each instance of request
    final String requestId =   rootRef.child("Food Request Restaurant").child(curtUserId).getKey();

    // make a query for all first 50 donation items in All Donations reference
    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 Donations")
                                .child(donateId)
                                .getValue();

                       /* if (s.equals(donateRest)) {   // s = string from reqRestLoc. Does not work
                            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) {
            }
        });

}

非常感谢!

0 个答案:

没有答案