Alex97
Alex97

Reputation: 466

Problem retrieving data from firebase database path problem - Android

I want to retrieve data from the database and insert it into a recycleview. The problem is that I don't know who I can get to the data inside the dataset.

First, take a look at my dataset structure: Json added below:

"Places" : {
"-M11-OIIHbKTVqCJ-Swv" : {
  "name" : "Farmacia Pagani Dr. Roberto",
  "others" : {
    "01" : {
      "Nome" : "Ambilify",
      "Price" : 100
    },
    "02" : {
      "Nome" : "Acetaminophoen",
      "Price" : 120
    }
  }
},
"-M11-OJJCGAPdJUknLXX" : {
  "name" : "FARMACIA DR. TUMIATTI MARIANO",
  "others" : {
    "01" : {
      "Nome" : "Ambilify",
      "Price" : 100
    },
    "02" : {
      "Nome" : "Acetaminophen",
      "Price" : 120
    }
  }
}
},

So what I want is: since I have the key "-M11-OIIHbKTVqCJ-Swv" I want to perform a search inside the object with this key. To do so I need to access Places/key object(-M11-OIIHbKTVqCJ-Swv)/others and then to perform a search based on Nome(means name in Italian). I want to return Nome: Ambilify Price : 100

This is what I did :

 private void firebaseUserSearch(String searchText) {
    DatabaseReference  searchforData = FirebaseDatabase.getInstance().getReference("Places");

    Toast.makeText(FirebaseSearch.this, "Started Search", Toast.LENGTH_LONG).show();
    System.out.println("keyNameOfFarmacy"+keyNameOfFarmacy);
    Query firebaseSearchQuery = searchforData.orderByChild(keyNameOfFarmacy).orderByChild("others").orderByChild("Nome").startAt(searchText).endAt(searchText + "\uf8ff");
    FirebaseRecyclerAdapter<DataDetails, UsersViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<DataDetails, UsersViewHolder>(
            DataDetails.class,
            R.layout.list_layout,
            UsersViewHolder.class,
            firebaseSearchQuery
    ) {
        @Override
        protected void populateViewHolder(UsersViewHolder viewHolder, DataDetails model, int i) {
            viewHolder.getDetails(model.getNome(), model.getPrice());
            viewHolder.setDetails(model.getNome(), model.getPrice());
        }

    };
    mResultList.setAdapter(firebaseRecyclerAdapter);
}

I have a problem with this part of the code because: orderByChild can be used just once.

Can anyone tell me how can I get the data please, I lost the last 2 3 h searching how to do it and I didn't find anything. Thank you in advance.

Upvotes: 0

Views: 36

Answers (1)

Peter Haddad
Peter Haddad

Reputation: 80914

Change this:

    Query firebaseSearchQuery = searchforData.orderByChild(keyNameOfFarmacy).orderByChild("others").orderByChild("Nome").startAt(searchText).endAt(searchText + "\uf8ff");

into this:

    Query firebaseSearchQuery = searchforData.child(keyNameOfFarmacy).child("others").orderByChild("Nome").startAt(searchText).endAt(searchText + "\uf8ff");

You already have access to others and to the key so just use child for that and orderByChild for Nome.

Upvotes: 1

Related Questions