title: How to restrict the duplicate entries in the Firebase Database
body: <p>In my app my user is entering the category and that category is stored in the <strong>FirebseDB</strong>. After storing the value in the DB the user can view the inserted value in the Spinner.</p>
  
  <p>At this point all works fine. </p>
  
  <p>I want user to enter only unique value .so , if user wants to enter the value which is already there he should get a Toast.</p>
  
  <p><strong>Following is my activity_main.xml:</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      tools:context="com.example.lenovo.baicactivitytest.MainActivity"&gt;
  
      &lt;Spinner
      android:id="@+id/sp"
      android:layout_width="match_parent"
      android:layout_height="match_parent"/&gt;
  
      &lt;Button
          android:id="@+id/add_btn"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentBottom="true"
          android:layout_centerHorizontal="true"
          android:layout_marginBottom="70dp"
          android:text="Add" /&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p><strong>Following is my inputdialogue.xml:</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical" android:layout_width="match_parent"
      android:layout_height="match_parent"&gt;
      &lt;LinearLayout
          android:layout_width="fill_parent"
          android:layout_height="match_parent"
          android:layout_marginTop="?attr/actionBarSize"
          android:orientation="vertical"
          android:paddingLeft="15dp"
          android:paddingRight="15dp"
          android:paddingTop="50dp"&gt;
          &lt;android.support.design.widget.TextInputLayout
              android:id="@+id/nameLayout"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"&gt;
              &lt;EditText
                  android:id="@+id/nameEditText"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:singleLine="true"
                  android:hint= "Name" /&gt;
          &lt;/android.support.design.widget.TextInputLayout&gt;
          &lt;Button android:id="@+id/saveBtn"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:text="Save"
              android:clickable="true"
              android:background="@color/colorAccent"
              android:layout_marginTop="40dp"
              android:textColor="@android:color/white"/&gt;
      &lt;/LinearLayout&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <h1>Spacecraft.java</h1>
  
  <pre><code>package com.example.lenovo.baicactivitytest;
  
  /**
   * Created by LENOVO on 29-12-2017.
   */
  
  public class Spacecraft {
      String name;
      public Spacecraft() {
      }
      public String getName() {
          return name;
      }
      public void setName(String name) {
          this.name = name;
      }
  }
  </code></pre>
  
  <h1>FirebaseHelper</h1>
  
  <pre><code>package com.example.lenovo.baicactivitytest;
  
  import com.google.firebase.database.ChildEventListener;
  import com.google.firebase.database.DataSnapshot;
  import com.google.firebase.database.DatabaseError;
  import com.google.firebase.database.DatabaseException;
  import com.google.firebase.database.DatabaseReference;
  
  import java.util.ArrayList;
  
  /**
   * Created by LENOVO on 29-12-2017.
   */
  
  public class FirebaseHelper {
      DatabaseReference db;
      Boolean saved = null;
      public FirebaseHelper(DatabaseReference db) {
          this.db = db;
      }
      //SAVE
      public  Boolean save(Spacecraft spacecraft)
      {
          if(spacecraft==null)
          {
              saved=false;
          }else
          {
              try
              {
                  db.child("Spacecraft").push().setValue(spacecraft);
                  saved=true;
              }catch (DatabaseException e)
              {
                  e.printStackTrace();
                  saved=false;
              }
          }
          return saved;
      }
      //READ
      public ArrayList&lt;String&gt; retrieve()
      {
          final ArrayList&lt;String&gt; spacecrafts=new ArrayList&lt;&gt;();
          db.addChildEventListener(new ChildEventListener() {
              @Override
              public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                  fetchData(dataSnapshot,spacecrafts);
              }
              @Override
              public void onChildChanged(DataSnapshot dataSnapshot, String s) {
                  fetchData(dataSnapshot,spacecrafts);
              }
              @Override
              public void onChildRemoved(DataSnapshot dataSnapshot) {
              }
              @Override
              public void onChildMoved(DataSnapshot dataSnapshot, String s) {
              }
              @Override
              public void onCancelled(DatabaseError databaseError) {
              }
          });
          return spacecrafts;
      }
      private void fetchData(DataSnapshot snapshot,ArrayList&lt;String&gt; spacecrafts)
      {
          spacecrafts.clear();
          for (DataSnapshot ds:snapshot.getChildren())
          {
              String name=ds.getValue(Spacecraft.class).getName();
              spacecrafts.add(name);
          }
      }
  }
  </code></pre>
  
  <h1>MainActivity.java</h1>
  
  <pre><code>package com.example.lenovo.baicactivitytest;
  
  import android.app.Dialog;
  import android.support.v7.app.AppCompatActivity;
  import android.os.Bundle;
  import android.view.View;
  import android.widget.ArrayAdapter;
  import android.widget.Button;
  import android.widget.EditText;
  import android.widget.Spinner;
  import android.widget.Toast;
  import com.google.firebase.database.DatabaseReference;
  import com.google.firebase.database.FirebaseDatabase;
  
  public class MainActivity extends AppCompatActivity {
      DatabaseReference db;
      FirebaseHelper helper;
      private Button madd_btn;
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          Spinner sp =(Spinner) findViewById(R.id.sp);
          //SETUP FB
          db= FirebaseDatabase.getInstance().getReference();
          helper = new FirebaseHelper(db);
          ArrayAdapter&lt;String&gt;  adapter = new ArrayAdapter&lt;String&gt;(this,android.R.layout.simple_list_item_1,helper.retrieve());
          adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
          sp.setAdapter(adapter);
          madd_btn = (Button) findViewById(R.id.add_btn);
          madd_btn.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  displayInputDialog();
              }
          });
      }
      //DISPLAY INPUT DILAOG
      private void displayInputDialog()
      {
          Dialog d=new Dialog(this);
          d.setTitle("Firebase database");
          d.setContentView(R.layout.inputdialog);
          final EditText nameTxt= (EditText) d.findViewById(R.id.nameEditText);
          Button saveBtn = (Button) d.findViewById(R.id.saveBtn);
          //SAVE
          saveBtn.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  //GET DATA
                  String name=nameTxt.getText().toString();
                  //set data
                  Spacecraft s = new Spacecraft();
                  s.setName(name);
                  //SAVE
                  if(name != null &amp;&amp; name.length()&gt;0)
                  {
                      if(helper.save(s))
                      {
                          nameTxt.setText("");
                      }
                  }else
                  {
                      Toast.makeText(MainActivity.this, "Name Cannot Be Empty", Toast.LENGTH_SHORT).show();
                  }
              }
          });
          d.show();
      }
  }
  </code></pre>