Check if there is a value in the database

1

I try to check a value that I enter with a EditText in the Firebase database before writing the value.

For this I have the following:

EditText Username;
Button buttonUser;
public FirebaseDatabase database;
boolean flag=false;
final int Posx = (int) (Math.random() * 1024);
final int Posy = (int) (Math.random() * 512);
public long fechamili=System.currentTimeMillis();


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

    buttonUser = (Button) findViewById(R.id.button_userlogin);
    Username = (EditText) findViewById(R.id.editTextUsername);
    database = FirebaseDatabase.getInstance();
    DatabaseReference Usuariosregistrados=database.getReference().child("usuarios").child("userID").child("nombre");


    Usuariosregistrados.orderByValue().addChildEventListener(new ChildEventListener() {

        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String prevChildKey) {

            Usuario usernameTaken=dataSnapshot.getValue(Usuario.class);
            String Nombre=Username.getText().toString().trim();
            Log.i( "NOMBRES", "los nombres obtenidos son" + usernameTaken);
            Log.i( "DATOS", " el nombre metido es" + Nombre);


            if(Nombre.equals(usernameTaken)){
                flag = true;

            }
            if(!flag) {
                crearusuario();
            }else{
                Toast.makeText (userlogin2.this, "Username already taken", Toast.LENGTH_LONG).show();}

        }

But I do not receive the values from the database to compare them. Can someone help me out?

    
asked by David Aranda 03.07.2017 в 13:52
source

2 answers

1

Adding to what Juan says, you can also use exists () to check if the data where you are looking for it is or not

DatabaseReference reference = FirebaseDatabase.getInstance().getReference("usuarios/"+id+"/nombre");
    reference.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
         if(dataSnapshot.exists())
             //existe
           else
          //no existe


        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
    
answered by 28.07.2018 в 19:05
0

If what you want is to get the information only once (as it seems to be the case) you should use addListenerForSingleValueEvent() as opposed to childAddesListener this is executed even if there are no children in the database node, in whose case the DataSnapshot object that arrives is null, that way you can check.

Example:

 DatabaseReference reference = FirebaseDatabase.getInstance().getReference("usuarios/"+id+"/nombre");
    reference.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
         if(dataSnapshot == null){
          // el nodo no tiene hijos
        }else{
        //hacer algo con el valor
        }

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
    
answered by 03.07.2017 в 18:50