0

I am trying to perform a query which will search for all the data under codes child, and if there is a positive result, I would also like to know the child name which that value is in.

enter image description here

Button bt = (Button)findViewById(R.id.button);
        bt.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                EditText input = (EditText) findViewById(R.id.editText);
                String string = input.getText().toString();
                Firebase rootRef = new Firebase("https://akiba-c9600.firebaseio.com/codes");
                Query queryRef = rootRef.orderByKey().equalTo(String.valueOf(input));
            }
        });

For example if I am querying for HUIYIU I would like to know that it is under the a 20mg child. How do I achieve this??

KENdi
  • 7,576
  • 2
  • 16
  • 31
Mr Okiri
  • 3
  • 7

2 Answers2

0

change

  Query queryRef = rootRef.orderByKey().equalTo(String.valueOf(input));

to

  Query queryRef = rootRef.orderByKey().equalTo(string);

then implement

queryRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                    // here you will get searched data
            }
       }
Kamlakar Kate
  • 397
  • 1
  • 6
  • 11
  • thanks so much but the question is still pending.. i want the child name under which that value is stored. please if you dont mind – Mr Okiri Jul 10 '17 at 06:41
  • if you know the structure, you can get child name easily.https://stackoverflow.com/questions/41951877/how-to-search-data-in-firebase – Kamlakar Kate Jul 10 '17 at 06:46
  • hey the question link does not help much..how do i get the child name of the value i just queried – Mr Okiri Jul 10 '17 at 07:17
0

Please use this code:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference codesRef = rootRef.child("codes");
ValueEventListener eventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        if(dataSnapshot.child("aa 20mg").child("HUIYIU").exists()) { //Check if exists
            String HUIYIU = dataSnapshot.child("aa 20mg").child("HUIYIU").getValue(String.class);
            String HUTERT = dataSnapshot.child("aa 20mg").child("HUTERT").getValue(String.class);
            Log.d("TAG", HUIYIU + " / " + HUTERT);
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
codesRef.addListenerForSingleValueEvent(eventListener);

And your output will be:

10 / 10
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193