简体   繁体   中英

How to get specific values from firebase realtime database.Similar to a where clause in sql

Am kind of new to android and firebase but I need to get some values from my firebase realtime database similar to a select stmt in sql(select address,name from All_machines where terminal_id = "terminal_id"). When i did the code below i was getting a null pointer exeception. But I guess am yet to understand the concept.

mDatabase = FirebaseDatabase.getInstance().getReference("All Machines");
mDatabase.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        try {
            for (DataSnapshot childDataSnapshot : dataSnapshot.getChildren()) {
                if (childDataSnapshot.getValue() != null){
                    try {
                        if (childDataSnapshot.child("terminal_id").getValue().toString().equals(newTerminal_id)){
                            String Terminal_address= ""+ childDataSnapshot.child("address").getValue();
                            String Terminal_name = ""+ childDataSnapshot.child("terminal_name").getValue();
                            terminal_name.setText(Terminal_name);
                            terminal_address.setText(Terminal_address);
                        }
                        else {
                            Toast.makeText(getApplicationContext(),"No Entry matching barcode",Toast.LENGTH_LONG).show();
                        }
                        //Log.v(TAG,""+ childDataSnapshot.child("terminal_name").getValue());   //gives the value for given keyname
                    }catch (Exception e){
                        Toast.makeText(getApplicationContext(),e.getMessage(),Toast.LENGTH_LONG).show();
                    }
                }else{
                    Toast.makeText(getApplicationContext(),"It's null.",Toast.LENGTH_LONG).show();
                }
                //Log.v(TAG,""+ childDataSnapshot.getKey()); //displays the key for the node
                //get the terminal id for each child then check if it matches the scanned barcode

            }

        }catch (Exception e){
            Toast.makeText(getApplicationContext(),e.getMessage(),Toast.LENGTH_LONG).show();
        }

    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {

    }
});

Database Structure:

{
  "All Machines": {
    "-M-JnXkserObKnDj3iZO": {
      "address": "OANDO IDIROKO",
      "atmClass": "6627",
      "bankName": "SPL",
      "brand": "NCR",
      "id": "-M-JnXkserObKnDj3iZO",
      "ip": "1033944",
      "latitude": "10333",
      "longitude": "10333",
      "serial_no": "13528891",
      "terminal_Id": "10332368",
      "terminal_name": "ATM9"
    }
  }
}

You can use orderByChild and equalTo methods to filter data in server side like below:

// Get DatabaseReference for All Machines
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference().child("All Machines");

// Use orderByChild to order data by terminal_Id and use equalTo for filter by newTerminal_id
Query query = databaseReference.orderByChild("terminal_Id").equalTo(newTerminal_id);

query.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        for(DataSnapshot childSnapshot: dataSnapshot.getChildren()) {

            String Terminal_address= childSnapshot.child("address").getValue(String.class);
            String Terminal_name = childSnapshot.child("terminal_name").getValue(String.class);

            terminal_name.setText(Terminal_name);
            terminal_address.setText(Terminal_address);
        }
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {

    }
});

Output: If terminal_Id equals 10332368 , then

Terminal_address = "OANDO IDIROKO";
Terminal_name = "ATM9";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM