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?