How to compare two different Firebase database child items f

怎甘沉沦 提交于 2020-01-06 01:56:23

问题


I need help in order to implement a functionality in my app such that if the user chooses a restaurant, a list of items that are available in the restaurant are displayed and if there are none, it displays a text and a button.

I went about it like this:I would compare two child items, the restaurant names to check if they are equal.If they are, it displays a recyclerview with a list of the items available.

Here is a screenshot of my firebase database structure

My goal is to 1. compare if the string at donationplceTxt (under the All Donations child) is equal to the string at place (under Food Request Restaurant).

Here's a code snippet of where I get the restaurant name from. Note that it is in another activity called RequestActivity.java. The actual comparison is done when I want to display the items from the database in a recycler view which is inside DisplayItems.java

Code snippet for RequestActivity.java which handles the place

 @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();
                }
            }
        }
    }

Code snippet from DisplayActivity.java where I'm supposed to display the items available (if there are any) at that particular restuarant

// 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) {
            }
        });

}

Thanks a lot!

来源:https://stackoverflow.com/questions/51698447/how-to-compare-two-different-firebase-database-child-items-f

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!