0

I want to ask, so I want to search a value which are an ID barcode on firebase. I try search on stackoverflow which using this line code.

 tindakan = FirebaseDatabase.getInstance().getReference("tindakan");
 bedahMulut = tindakan.child("Bedah Mulut");

 Query searchByID = tindakan.orderByChild("ID").equalTo(idBarcode);

 searchByID.addValueEventListener(new ValueEventListener() {
     @Override
     public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
         for (DataSnapshot idSnapshot : dataSnapshot.getChildren()) {
             Log.v("hasil", "Found! -> " + idSnapshot.child("ID").getValue(String.class));
         }
     }

     @Override
     public void onCancelled(@NonNull DatabaseError databaseError) {
         Log.v("hasil", databaseError.toException().toString());
     }
 });

but this code are return null value, I already post structure firebase and debug session in below. Please help me in advance, thank you :)

here is structure on firebase

Debug session, that are showing null value

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Can you expand your database to show `ID` for **Bedah Mulut**? Beside this `tindakan` and `bedahMulut` don't have children like `ID` which is accessible through `orderByChild` – Md. Asaduzzaman Feb 19 '20 at 12:56
  • Please add what Md. Asaduzzaman asked for and please also respond with @AlexMamo – Alex Mamo Feb 19 '20 at 13:26

1 Answers1

0

It looks like you're trying to query a deeply nested data structure, which is not possible. Firebase Realtime Database queries operate on a flat list, where the property/value you order/filter on is at a fixed path under each direct child node.

The solution is typically to create an additional data structure, where you keep a simple list of IDs and then the value is the path of the node that has that ID.

"IDs": {
  "INLAY": "/tindakan/Konservasi/Inlay/Kelas -"
}

See

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • If the structure like that, can I listing only just the name? Because on the project I can see (by name for example inlay (not an ID barcode)), add, update, and remove the data. – Kazuto Rito Feb 19 '20 at 15:58
  • For example if I want to edit, it must show the name first (for example inlay) and then show the class and the show all information (like ID, name, price) – Kazuto Rito Feb 19 '20 at 16:02
  • I'm not sure I understand what you're asking there. But the structure can be whatever you want to look up, or search on. The structure in this answer makes it easy to find the path(s) for `INLAY`, but there are many other variants in the questions I linked. – Frank van Puffelen Feb 19 '20 at 16:02
  • And then user can update the data or remove the data. If user want to add another data it will go to another section. – Kazuto Rito Feb 19 '20 at 16:03
  • So you recommend is to ID first then explain the rest? Which I must split the string to get each information? Right? – Kazuto Rito Feb 19 '20 at 16:06
  • Because it's not only search the data – Kazuto Rito Feb 19 '20 at 16:06
  • I already try using your method and it WORKS for all function like create, update, even delete from firebase! Do you have any suggestion or can you tell me, how to choose to be parent (for example in this case you choose "ID" as parent)? – Kazuto Rito Feb 25 '20 at 14:31
  • 1
    I already check the mark, but I can't up vote because my reputation below 15 – Kazuto Rito Feb 26 '20 at 13:58