To use the name as the key of the node, instead of using a push ID, replace:
reff.push().setValue(member);
With:
reff.child(txtname.getText().toString().trim()).setValue(member);
With that change, you can check if a name is already in use with:
ref.child("isThisNameInUse").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
... the user exists already
}
else {
... the user does not exist yet
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException();
}
}
Note that the topic of checking whether a user name exists has been covered quite a few times already, so I recommend checking out some of the other relevant questions here and here.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…