嗨,Android开发新手,请在此选择一个用户名活动,我想要app来检查firebase数据库中是否存在用户名,如果为true,则显示对话框并且不向数据库添加值,如果为false add。到目前为止,如果它是真的,它继续添加到firebase数据库..我做错了什么?
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_check);
mAuth = FirebaseAuth.getInstance();
pd = new ProgressDialog(this);
FirebaseUser curr_user = FirebaseAuth.getInstance().getCurrentUser();
String uid = null;
if (curr_user != null) {
uid = curr_user.getUid();
mDatabase = FirebaseDatabase.getInstance().getReference().child("Users").child(uid);
cUser = (TextInputLayout) findViewById(R.id.cUserr);
cBut = (Button) findViewById(R.id.cCon);
TextView backArrow = (TextView) findViewById(R.id.home);
backArrow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent mainintent = new Intent(CheckUsername.this, MainActivity.class);
mainintent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(mainintent);
finish();
}
});
这里我调用了检查用户名是否存在方法
cBut.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "onClick: attempting to save changes.");
checkIfUsernameExists("");
}
});
}
}
private void saveProfileSettings() {
final String username = cUser.getEditText().toString();
//case1: if the user made a change to their username
if (TextUtils.isEmpty(username)) {
cUser.setError("Empty Field");
} else {
// checkIfUsernameExists(username);
}
}
这里我检查是否存在,如果是真正的呼叫提醒对话但似乎不是todo,它所做的只是创建数据库中的值(如果它存在)
private void checkIfUsernameExists(final String username) {
Log.d(TAG, "checkIfUsernameExists: Checking if " + username + " already exists.");
DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
Query query = reference
.child(getString(R.string.dbname_users))
.orderByChild(getString(R.string.field_username))
.equalTo(username);
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
//add the username
updateUsername(username);
Toast.makeText(CheckUsername.this, "saved username.", Toast.LENGTH_SHORT).show();
}
for (DataSnapshot singleSnapshot : dataSnapshot.getChildren()) {
if (!singleSnapshot.exists()) {
Log.d(TAG, "checkIfUsernameExists: FOUND A MATCH: " + singleSnapshot.getValue(Users.class).getName());
request_code();
Toast.makeText(CheckUsername.this, "That username already exists.", Toast.LENGTH_SHORT).show();
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
这是在check username方法中调用的警告对话框方法,但它似乎不起作用。
public void request_code(){
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Pls Enter a Different Username");
alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
alert.show();
}
}
答案 0 :(得分:0)
如果你使USERNAME是唯一的,那么我想只是
DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
Query query = reference
.child(getString(R.string.dbname_users))
.orderByChild(getString(R.string.field_username))
.equalTo(username);
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
//add the username
updateUsername(username);
Toast.makeText(CheckUsername.this, "saved username.", Toast.LENGTH_SHORT).show();
}
else
{
request_code();
}
}
}