3

I am using firebase. I want to check for duplicate data for my application. Currently I am letting user to enter name and email. But if the user enters the same name and email for the second time it also get added to database. how to solve this Here is my code:

package com.example.zaid_pc.mpd2;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

public class tournament extends AppCompatActivity {

private EditText etTournament;
private EditText emailTournament;
private DatabaseReference mDatabaseRefernce;
Button btnTournament;



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tournament);

etTournament = (EditText)findViewById(R.id.etTournament);
emailTournament = (EditText)findViewById(R.id.emailTournament);
btnTournament = (Button)findViewById(R.id.btnTournament);

mDatabaseRefernce = FirebaseDatabase.getInstance().getReference().child("People Registered");

 btnTournament.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View view) {



  final String name = etTournament.getText().toString().trim();
  final String email = emailTournament.getText().toString().trim();

  //mDatabaseRefernce.child("Name").setValue(name);
  //mDatabaseRefernce.child("Email").setValue(email);



final DatabaseReference peopleRegisered = mDatabaseRefernce.push();
peopleRegisered.child("Name").setValue(name);
peopleRegisered.child("Email").setValue(email);



Toast.makeText(tournament.this, "You will be notified via email", Toast.LENGTH_LONG).show();

finish();




        }
    });


}

}

AL.
  • 36,815
  • 10
  • 142
  • 281
zaid mohammad
  • 67
  • 2
  • 10

2 Answers2

1

Before pushing data to database first check with database if data exist or not

check username is existing or not from following code

DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
Query query = reference.child("issue").orderByChild("id").equalTo(0);
query.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        if (dataSnapshot.exists()) {
            // dataSnapshot is the "issue" node with all children with id 0
            for (DataSnapshot issue : dataSnapshot.getChildren()) {
                // do something with the individual "issues"
            }
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
});

modified this code based on your need

for ref Firebase querying data

Community
  • 1
  • 1
Lingeshwaran
  • 579
  • 4
  • 15
0

To solve this problem i recomand you using for each user a unique identifier. This identifier can be, a unique id generated by the push() method, the uid or the email address. I suggest you the last option. Because Firebase does not accept a symbol like . in the key, you'll need to econde the email address like this:

name@email.com -> name@email,com

To achieve this, here are 2 methods that will help you encode and decode the email address.

private String encodeUserEmail(String userEmail) {
    return userEmail.replace(".", ",");
}

private String decodeUserEmail(String userEmail) {
    return userEmail.replace(",", ".");
}

Assuming you database structure is looking like this:

root
 |
 --- users
        |
        --- name@email,com
                |
                --- uid: 4362454274745
                |
                --- name: John

you can verify if a user exists using the following code:

DatabaseReference userEmailRef = FirebaseDatabase.getInstance().getReference().child("users").child(userEmail);
    ValueEventListener valueEventListener = new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            if (dataSnapshot.exists()) {
                Log.d("TAG", "Users exists!");
            } else {
                //Create user
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {}
    };
    userEmailRef.addListenerForSingleValueEvent(valueEventListener);

In which userEmail is the encoded email of the user.

Hope it helps.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193