so I have a database where I search for products by barcodes.
I wanted to have a check to see if the products are in my database
mFirebaseInstance = FirebaseDatabase.getInstance();
mFirebaseDatabase = mFirebaseInstance.getReference("ProductData");
productId = mFirebaseDatabase.push().getKey();
public void searchProduct(String barcode){
Query barcodeQuery = mFirebaseDatabase.child("Products")
.orderByChild("barcode").equalTo(barcode);
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String name = ds.child("name").getValue(String.class);
int price = ds.child("price").getValue(Integer.class);
int quantity = ds.child("quantity").getValue(Integer.class);
Log.d("DATABASE", name + "/" + price + "/" + quantity); /// The output that I get is this: Neopraxam/25/1 and Vazelina/250/1
That's how I search for the products by barcode.
searchProduct("123123123");
searchProduct("3232");
I have tried to add these methods to check if the value I'm trying to get is null:
if (ds.exists()){} OR
if (ds.getChildrenCount() != 0){}
But I have seen that it doesn't even enter in the loop if the value doesn't exist. So I'm assuming that It's coming from the query. So, how can I check if the query returns a null value because I used the method .equalTo(barcode) so I suppose that it should return a true or a false value