0

I want fetch the data from Firebase realtime database using multiple where condition.

databaseReference = FirebaseDatabase.getInstance().getReference("tblSalesDetails");
Query query = databaseReference.child("authenticationId").child("9591290928").orderByChild("invoiceNo").equalTo(str_invoice_no);
query.addValueEventListener(new ValueEventListener() {

    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        int i=0;
        for(DataSnapshot snap : dataSnapshot.getChildren()) {
            Log.d("tag","item ID:");


            TableRow tr = new TableRow(AddItemToSale.this);
            CheckBox Box = new CheckBox(AddItemToSale.this);
            tr.setLayoutParams(getLayoutParams());

            Log.d("tag","item ID:"+snap.child("itemId").getValue(String.class));



            tr.addView(getTextView("0", String.valueOf(i + 1), ContextCompat.getColor(AddItemToSale.this, R.color.colorPrimary), Typeface.NORMAL, R.drawable.high));

            tr.addView(getTextView((snap.child("sdId").getValue(String.class)),snap.child("itemId").getValue(String.class), ContextCompat.getColor(AddItemToSale.this, R.color.colorPrimary), Typeface.NORMAL,R.drawable.high));
            tr.addView(getTextView((snap.child("sdId").getValue(String.class)),snap.child("amount").getValue(String.class), ContextCompat.getColor(AddItemToSale.this, R.color.colorPrimary), Typeface.NORMAL,R.drawable.high));
            tr.addView(getTextView((snap.child("sdId").getValue(String.class)),snap.child("discAmount").getValue(String.class), ContextCompat.getColor(AddItemToSale.this, R.color.colorPrimary), Typeface.NORMAL, R.drawable.high));
            tr.addView(getTextView((snap.child("sdId").getValue(String.class)),snap.child("taxName").getValue(String.class), ContextCompat.getColor(AddItemToSale.this, R.color.colorPrimary), Typeface.NORMAL, R.drawable.high));
            tbl_sales.addView(tr, getTblLayoutParams());

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

    }
});
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
pooja
  • 12
  • 2

1 Answers1

0

The query that you are using is getting you only a single invoice where the invoiceNo property is equal to str_invoice_no. There is no way you can use multiple "WHERE" clauses in Firebase realtime database. The only solution in which you can filter a property on multiple values in Firebase realtime database is if those invoice numbers are in a range. For instance, you can get all invoices matching some invoice numbers using the following query:

databaseReference = FirebaseDatabase.getInstance().getReference("tblSalesDetails");
Query query = databaseReference.child("authenticationId").child("9591290928")
    .orderByChild("invoiceNo")
    .startAt("startInvoiceNo")
    .endAt("endInvoiceNo");

Please note, there is no way in Firebase realtime database nor in the new Cloud Firestore database to pass multiple separate values into a query. You will have to execute a separate query for each value and merge them client-side.

Please also check Frank van Puffelen's answer from the following post:

And for Firestore, please also check my answer from this post:

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • i dont want range i want to fetch the data based on single invoice number with authentication id – pooja Apr 24 '19 at 09:58
  • Isn't your actual code doing what you want? Isn't getting a single invoice? As I see, your actual question is: "How to fetch the data from Firebase database using **multiple where condition** in Android". So please be more specific regarding what you are looking to achieve. – Alex Mamo Apr 24 '19 at 10:10