title: How to check existed data in firebase?
body: <p>I am using firebase database, but I am newbie with it. I am following this <a href="http://www.androidhive.info/2016/10/android-working-with-firebase-realtime-database/" rel="nofollow noreferrer">tutorial</a>,</p>
  
  <p>Now, my question is how can I check the email if it is already exist in the database? If email is already there data should not be add to db.</p>
  
  <pre><code> public class MainActivity extends AppCompatActivity {
  
      private static final String TAG = MainActivity.class.getSimpleName();
      private TextView txtDetails;
      private EditText inputName, inputEmail;
      private Button btnSave;
      private DatabaseReference mFirebaseDatabase;
      private FirebaseDatabase mFirebaseInstance;
  
      private String userId;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          // Displaying toolbar icon
          getSupportActionBar().setDisplayShowHomeEnabled(true);
          getSupportActionBar().setIcon(R.mipmap.ic_launcher);
  
          txtDetails = (TextView) findViewById(R.id.txt_user);
          inputName = (EditText) findViewById(R.id.name);
          inputEmail = (EditText) findViewById(R.id.email);
          btnSave = (Button) findViewById(R.id.btn_save);
  
          mFirebaseInstance = FirebaseDatabase.getInstance();
  
          // get reference to 'users' node
          mFirebaseDatabase = mFirebaseInstance.getReference("users");
  
          // store app title to 'app_title' node
          mFirebaseInstance.getReference("app_title").setValue("Realtime");
  
          // app_title change listener
          mFirebaseInstance.getReference("app_title").addValueEventListener(new ValueEventListener() {
              @Override
              public void onDataChange(DataSnapshot dataSnapshot) {
                  Log.e(TAG, "App title updated");
  
                  String appTitle = dataSnapshot.getValue(String.class);
  
                  // update toolbar title
                  getSupportActionBar().setTitle(appTitle);
              }
  
              @Override
              public void onCancelled(DatabaseError error) {
                  // Failed to read value
                  Log.e(TAG, "Failed to read app title value.", error.toException());
              }
          });
  
  
  
          // Save / update the user
          btnSave.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                 final String name = inputName.getText().toString();
                  final String email = inputEmail.getText().toString();
  
                  // Check for already existed userId
                  if (TextUtils.isEmpty(userId)) {
                      createUser(name, email);
                  } else {
                      updateUser(name, email);
                  }
  
  
                  mFirebaseDatabase.addListenerForSingleValueEvent(new ValueEventListener() {
                      @Override
                      public void onDataChange(DataSnapshot dataSnapshot) {
                          //Your Logic here
                          for (DataSnapshot eventSnapshot : dataSnapshot.getChildren()) {
                              User mModel = eventSnapshot.getValue(User.class);
                             // Log.e("DATA" ,""+ mModel.getName());
  
                          }
  
  
  
                      }
  
                      @Override
                      public void onCancelled(DatabaseError databaseError) {
  
                      }
                  });
              }
          });
  
          toggleButton();
      }
  
      // Changing button text
      private void toggleButton() {
          if (TextUtils.isEmpty(userId)) {
              btnSave.setText("Save");
          } else {
              btnSave.setText("Update");
          }
      }
  
      private void filter()
      {
  
      }
      /**
       * Creating new user node under 'users'
       */
      private void createUser(String name, String email) {
          // TODO
          // In real apps this userId should be fetched
          // by implementing firebase auth
          if (TextUtils.isEmpty(userId)) {
              userId = mFirebaseDatabase.push().getKey();
          }
  
          User user = new User(name, email);
  
          mFirebaseDatabase.child(userId).setValue(user);
  
          addUserChangeListener();
      }
  
      /**
       * User data change listener
       */
      private void addUserChangeListener() {
          // User data change listener
          mFirebaseDatabase.child(userId).addValueEventListener(new ValueEventListener() {
              @Override
              public void onDataChange(DataSnapshot dataSnapshot) {
                  User user = dataSnapshot.getValue(User.class);
  
  
  
                  if(!dataSnapshot.child("users").child("email").exists())
                  {
                      Log.e(TAG, "User not exists");
                  }
                  else
                  {
                      Log.e(TAG, "User exists");
                  }
  
                  // Check for null
                  if (user == null) {
                      Log.e(TAG, "User data is null!");
                      return;
                  }
  
                  Log.e(TAG, "User data is changed!" + user.name + ", " + user.email);
  
                  // Display newly updated name and email
                  txtDetails.setText(user.name + ", " + user.email);
  
                  // clear edit text
                  inputEmail.setText("");
                  inputName.setText("");
  
                  toggleButton();
              }
  
              @Override
              public void onCancelled(DatabaseError error) {
                  // Failed to read value
                  Log.e(TAG, "Failed to read user", error.toException());
              }
          });
      }
  
      private void updateUser(String name, String email) {
          // updating the user via child nodes
          if (!TextUtils.isEmpty(name))
              mFirebaseDatabase.child(userId).child("name").setValue(name);
  
          if (!TextUtils.isEmpty(email))
              mFirebaseDatabase.child(userId).child("email").setValue(email);
      }
  }
  </code></pre>