Chris
Chris

Reputation: 73

How can I validate / check if string value exists in Firebase database (Android)

I've created AutoCompleteTextView for Ingredient, it works perfect. The DB save also works, I've tested it without validation. If Ingredient exists in Firebase db I want to add recipe to DB. For example if RecipeIngredients="milk" recipe should be saved to DB.

Here's my code.

DatabaseReference ingDBref = FirebaseDatabase.getInstance().getReference("Ingredients");
DatabaseReference databaseReference=FirebaseDatabase.getInstance().getReference("Recipes");

public void addRecipe(){
        final String RecipeName=RecipeNameEditText.getText().toString();
        final String RecipePrepareTime=PrepareTimeEditText.getText().toString();        
        final String RecipeIngredients=IngredientsACTV.getText().toString();
        final String RecipeDescription=DescriptionEditText.getText().toString();
        final String RecipeDifficulty=DifficultySpinner.getSelectedItem().toString();
        key=databaseReference.push().getKey();

        ingDBref.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                for(DataSnapshot data: dataSnapshot.getChildren()){
                    if(data.child(RecipeIngredients).getValue()!=null){                   

                        Recipes recipes=new Recipes(key,RecipeName,RecipeDifficulty,RecipePrepareTime,RecipeIngredients,RecipeDescription);
                        databaseReference.child(key).setValue(recipes);
                        Toast.makeText(AddRecipeActivity.this,"Saved...",Toast.LENGTH_LONG).show();

                    }
                    else{
                        IngredientsACTV.setText("");
            Toast.makeText(AddRecipeActivity.this,"Error...",Toast.LENGTH_LONG).show();
                    }
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

Firebase DB

enter image description here

Upvotes: 2

Views: 1204

Answers (3)

Shaifali Rajput
Shaifali Rajput

Reputation: 1279

Try this

mDatabase.child("Ingredients")
                    .addChildEventListener(new ChildEventListener() {
                        @Override
                        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                            Map<String, String> map = (Map<String, String>) dataSnapshot.getValue();

                          String value=map.get("IngredientName"); 
                          // here will be your value of IngredientName

                          if(value.equals("Milk"){
                           // do something
                           }
                        }

                        @Override
                        public void onChildChanged(DataSnapshot dataSnapshot, String s) {
                            myDialog.CancelProgressDialog();
                        }

                        @Override
                        public void onChildRemoved(DataSnapshot dataSnapshot) {
                            myDialog.CancelProgressDialog();
                        }

                        @Override
                        public void onChildMoved(DataSnapshot dataSnapshot, String s) {
                            myDialog.CancelProgressDialog();
                        }

                        @Override
                        public void onCancelled(DatabaseError databaseError) {
                            myDialog.CancelProgressDialog();
                        }
                    });

Upvotes: 0

Chris
Chris

Reputation: 73

if(data.child("ingredientName").getValue().equals(RecipeIngredients))

Solved my problem.

Upvotes: 3

Alex Mamo
Alex Mamo

Reputation: 138824

This is happening because you are looping through the whole object. If the condition has been met, then you you set that value, if not then the else part is triggered for each of the remaining elements.

To solve this you can remove the else part if not needed or you need to use the exists() method provided by Firebase. To achieve this, just attach a listener on the same database reference and check if that child exists.

Hope it helps.

Upvotes: 0

Related Questions