0

That's my Firebase Structure

My code

FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("stock");
Query i = myRef.orderByChild("keywords").equalTo("Hammer");
i.addValueEventListener(new ValueEventListener(){
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        System.out.println(dataSnapshot.getValue());    
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
});

My Problem: I wanna query only one item with the Keyword eg. "Hammer", but I only getting null as value. If I query like "orderByChild("keyword").equalTo("Hammer")" I'm getting the same null value. Please help me.

7
  • 1
    Please read this guide for asking questions over here: stackoverflow.com/help/how-to-ask Commented Jul 11, 2016 at 12:28
  • add the code as text. and include what myRefis refering to. Commented Jul 11, 2016 at 12:36
  • @adolfosrs there is no "stock" node in the database provided Commented Jul 11, 2016 at 13:05
  • Why would you use a realtime database to store this static data? It seems like overkill Commented Jul 11, 2016 at 13:08
  • "stock" is there, you only can't see it on the picture Commented Jul 11, 2016 at 13:08

1 Answer 1

1

orderByChild will only look into the next level childs for the branch you are refering in /stocks. Your code would only work if you had the child keywords/0/keyword right bellow /stocks. Since its under /stocks/inserate/0 it wont work.

Query i = myRef.child("inserate/0/keywords").orderByChild("keyword").equalTo("Hammer");

This will do the trick but it might not be exactly what you want. If you want to search in all the iserates childs you should do some refactoring into your database.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.