Mr. Benbenbenbenben
Mr. Benbenbenbenben

Reputation: 123

How to retrieve the sibling node's value in firebase?

What I'm trying to do is to return the value of the key named class of the node named m1 with a sibling named name


Here's what my database looked like

enter image description here


Here's what my code looked like

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference().child("Medicines");
        final Query userQuery = rootRef.orderByChild("name").equalTo(textView.getText().toString());

        userQuery.addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {

                String myParentNode = dataSnapshot.getKey();
                for (DataSnapshot child: dataSnapshot.getChildren())
                {
                    String key = child.getKey().toString();
                    String value = child.getValue().toString();

                    txt1.setText(myParentNode);

                    DatabaseReference childRef = FirebaseDatabase.getInstance().getReference().child(myParentNode);
                childRef.addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        String class_ = dataSnapshot.getValue().toString();         txt2.setText(class_);
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {}
                });
            }
            }

However it just gave me error T.T

Upvotes: 3

Views: 693

Answers (2)

birukhimself
birukhimself

Reputation: 193

Try this: Create a class called Medicine(If you don't already have one) and make it have two String variables name and class and create a constructor, getter and setter methods then use this method:

public String getClassOfMedicineById(String id){
    final String medicineClass = "";
    FirebaseDatabase.getInstance().getReference("medicine-handbook").child("Medicines")
            .child(id)
            .addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    Medicine medicine = dataSnapshot.getValue(Medicine.class);
                    medicineClass = medicine.get_class();
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });
    return medicineClass;
}

And then call it like: getClassOfMedicineById("m1"); Oh and don't call your class getter method getClass(). I think there's a method in the Object class so it might interfere or something.

Upvotes: 0

Peter Haddad
Peter Haddad

Reputation: 80914

Try this:

 DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference().child("Medicines");
    final Query userQuery = rootRef.orderByChild("name").equalTo(textView.getText().toString());

    userQuery.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot datas: dataSnapshot.getChildren()){
   String keys=datas.getKey();
   String class=datas.child("class").getValue().toString();
   String name=datas.child("name").getValue().toString();
  }
 }
    @Override
  public void onCancelled(DatabaseError databaseError) {
  }
  });

the datasnasphot is medicines then the query is orderByChild("name").equalTo(textView.getText().toString()); equal to the value that is in the textView.

String keys=datas.getKey(); this will get you the key can be m1 or m2 or m3 depending on the text that you retrieve from the textview.

Then the other two will retrieve the name and the class from the database that are under a specific key.

Upvotes: 1

Related Questions