Firebase Child Exist or Not in android Under Random key-ThrowExceptions

Exception or error:

I’m new at android developing. first of all look at the picture .

enter image description here

The problem I’m having right now, I can’t check child exist or not in “Daily Meal” portion. Where I can easily check child on “BazarList” because there is no random generated key.

Here is the code for child exist checking for “BazarList” and it’s works fine .

 DatabaseReference dr= FirebaseDatabase.getInstance().getReference("BazarList");

    dr.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {


     if (dataSnapshot.child(bzs.getDate()).exists()) {
                    ExistD();

                }
                else {



                        Bazar bz = new Bazar(dt, ct, nm, it);
                        dr.child(dt).setValue(bz);
                        dataInsert();

                    }
}

now my question is how can i check the child (date) of “Daily Meal” exist or not?
i’ve tried this

  DatabaseReference databaseBazars= FirebaseDatabase.getInstance().getReference("Daily Meal");

final MealE user = new MealE(
            md.getText().toString().trim(),
            rb.getText().toString().trim(),
            rb2.getText().toString().trim(),
            rb3.getText().toString().trim(),
            rb4.getText().toString().trim(),
            rb5.getText().toString().trim(),
            rb6.getText().toString().trim(),
            rb7.getText().toString().trim());


    databaseBazars.addListenerForSingleValueEvent(new ValueEventListener() {

                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {

                    if (dataSnapshot.child(user.getDate()).exists()) {


                       ExistD();

                    }


                    else {
                        if (!TextUtils.isEmpty(dt))
                        {
                            String id = databaseBazars.push().getKey();
                            MealE bz = new MealE(dt, s1, s2, s3, s4, s5, s6, s7);
                            databaseBazars.child(id).setValue(bz);
                            openDialog();
                        }
                        else {

                            MissingDAte();
                        }

                    }
                }


                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });





        }

here is a thing if you guyz thinking why i’m not doing the “Daily Meal” part Like “BazarList”? there is the answer. in that case firebase sorted data automatically that means if i insert 1 then 4 then 11 then in firebase it automatically sor like 1,11,4 . then the problem . when i’m trying to retrieve it’s not coming back sequentially. that’s why i need put those value in random generated keys.

How to solve:

Use a query equal to the date, which will show all children that fit the criteria.

Query dateQuery = FirebaseDatabase.getInstance().getReference("Daily Meal").orderByChild("date").equalTo(user.getDate());

dateQuery.addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                if (dataSnapshot.exists()) {
                   ExistD();
                } else {
                    if (!TextUtils.isEmpty(dt))
                    {
                        String id = databaseBazars.push().getKey();
                        MealE bz = new MealE(dt, s1, s2, s3, s4, s5, s6, s7);
                        databaseBazars.child(id).setValue(bz);
                        openDialog();
                    } else {
                        MissingDAte();
                    }
                }
            }
        // put rest of your code here
});

Check out the documentation to see more about queries.

###

change this:

 if (dataSnapshot.child(user.getDate()).exists()) {

into this:

 if (dataSnapshot.child("date").exists()) {

in the database the child under Daily Meal is called date and not it’s value.

Try this:

   Query dateQuery = FirebaseDatabase.getInstance().getReference("Daily Meal");

   dateQuery.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
                for(DataSnapshot data: dataSnapshot.getChildren()){
                     String keys=data.getKey();
                   if(dataSnapshot.child(keys).child("date").exists()){
                   ExistD();
                    }

                }
            }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

Leave a Reply

Your email address will not be published. Required fields are marked *