如何根据Android Studio中子项目的比较结果显示来自Firebase的数据?

时间:2018-08-08 03:04:57

标签: java android firebase firebase-realtime-database

我需要比较或检查在“请求”子项下“位置”保存的字符串的所有匹配项,这是Google Maps API中location的字符串值。

找到匹配项后,我想在适配器中显示donationItems的所有相应值。我已经尝试了所有可能的方法,但是结果却总是令人讨厌(大多数时候我什么都没得到)。

Here is a screenshot of my current firebase database structure

下面是JSON数据库。

{
  "Users" : {
    "s64OJGXN8UUPQ96Wvw4FnLbNMPt1" : {
      "Donations" : {
        "-LJMEDEyRFjFEyMSb-wD" : {
          "donationCode" : "UULDJFPIGUOE0",
          "donationItems" : "Fries  Nuggets  ",
          "donationQty" : 3,
          "donationplceTxt" : "Calzone's House of Pizza, 1164 W 6th St, Erie, PA 16507, USA"
        },
        "-LJMRPxf3JjjZ5Gu2aNe" : {
          "donationCode" : "JJLFIRIT99EJ",
          "donationItems" : "Burger  Nuggets  ",
          "donationQty" : 2,
          "donationplceTxt" : "Wendy's, 201 E 6th Ave, Denver, CO 80203, USA"
        },
        "-LJMRcaAHU_Jl2pI60TI" : {
          "donationCode" : "804J444JGJ04IJGGGKGIO",
          "donationItems" : "Fries  Burger  ",
          "donationQty" : 4,
          "donationplceTxt" : "Calzone's House of Pizza, 1164 W 6th St, Erie, PA 16507, USA"
        }
      },
      "Requests" : {
        "-LJMGOlrHPMLRMJtVhQ1" : {
          "place" : "Calzone's House of Pizza, 1164 W 6th St, Erie, PA 16507, USA"
        }
      }
    }
  }
}

这是捐赠时如何​​保存位置的代码段

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == PLACE_PICKER_REQUEST) {
        if (resultCode == RESULT_OK) {
            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());

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

// real-time adding to the firebase database
private void SubmitDonation(){

    String donatecode = donateCode.getText().toString();
    String donateitems = "";
    int donateqty = donateQty;
    String donateplceTxt = placeTxt.getText().toString();
    // donateplceTxt = donatePlcTxt;

    for(String Selections : selection){
        donateitems = donateitems + Selections + "  ";
    }

    if((TextUtils.isEmpty(donatecode))){

        Toast.makeText(this, "One or more field(s) missing!",Toast.LENGTH_LONG).show();
        // you can still sprlit these to check for each text field

    }else {
        FirebaseUser user = mAuth.getCurrentUser();
        userID = user.getUid();
        String id =  UserDonateRef.push().getKey();
        Donation donation = new Donation(donatecode,donateitems,donateqty,donateplceTxt);

        // myRef.child(id).setValue(dessert);
        UserDonateRef
                .child("Users")
                .child(userID)
                .child("Donations")
                .child(id)
                .setValue(donation);

        //Tell to the user to select an appropriate place
        Toast.makeText(this, "The item has been successfully donated!",Toast.LENGTH_LONG).show();
    }
}

我如何保存请求餐厅位置的代码与此类似。 在id下面的代码是我如何在recyclerview上显示项目的代码

// make these ui items invisible at first instance
    // will make them visible if there are no items to display
    // on recycler view
    noItemsTxt.setVisibility(View.GONE);
    OkExitBtn.setVisibility(View.GONE);

    final String curtUserId = FirebaseAuth.getInstance().getCurrentUser().getUid();

    final FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference rootRef = database.getReference().child("Users");


    // 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("Donations")
            .child(curtUserId)
            .orderByChild("donationplceTxt")
            .limitToFirst(10);

    // 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 个答案:

没有答案