你好朋友,我正在开发一个应用程序,它包含注册活动。在此活动中,应用需要用户名和来自用户的其他信息。我希望应用程序动态显示是否存在用户名。此外,用户类型的用户名和应用程序说它已经存在或不通过textview而没有按任何东西,甚至没有按钮。所以我的问题对我来说很复杂。我认为这是基本知识,所以我为此感到困惑。
当我将马丁放入必须包含用户名的edittext中时,textview向我显示“用户名已存在”。但是,当我输入其他用户名(例如franklin或不在数据库textview中的用户名)时,显示“您选择的用户名多么酷!”我明白为什么会这样。 For循环适用于用户中的每个用户名,并且在到达用户中的最后一个用户名时会自行结束。
我的Firebase数据库
{
"users" : {
"-LNalJeQbFdjPsAKGM8P" : {
"username" : "trevor"
},
"-LNalO92NTntHY_1Xu4T" : {
"username" : "michael"
},
"-LNalS9frQG-E4HlZD0f" : {
"username" : "franklin"
},
"-LNamHiSE6WHdwdTpvFM" : {
"username" : "lamar"
},
"-LNamLampo-FQrhGE0kA" : {
"username" : "martin"
}
}
}
我的Java类
package com.onurturali.paylass;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
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;
import java.util.Objects;
public class CreateProfileActivity extends AppCompatActivity {
Button createProfileAction;
EditText registerUsername, registerFullname, registerAge, registerCity, RegisterAbout;
TextView isUsernameValidOrNot;
DatabaseReference databaseReference;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_profile);
databaseReference = FirebaseDatabase.getInstance().getReference();
registerUsername = (EditText) findViewById(R.id.registerUsername);
registerFullname = (EditText) findViewById(R.id.registerFullname);
registerAge = (EditText) findViewById(R.id.registerAge);
registerCity = (EditText) findViewById(R.id.registerCity);
RegisterAbout = (EditText) findViewById(R.id.registerAbout);
createProfileAction = (Button) findViewById(R.id.createProfileAction);
isUsernameValidOrNot = (TextView) findViewById(R.id.isUsernameValidOrNot);
createProfileAction.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
// When username textbox string changed
registerUsername.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
databaseReference.child("users").orderByChild(charSequence.toString()).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot id:dataSnapshot.getChildren()){
String user_id = id.getKey();
databaseReference.child("users").child(Objects.requireNonNull(user_id)).child("username").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if(Objects.requireNonNull(dataSnapshot.getValue()).toString().equals(registerUsername.getText().toString())){
isUsernameValidOrNot.setText("Kullanıcı adı kullanımda. Başka bir tane demeneye ne dersin?");
isUsernameValidOrNot.setTextColor(getResources().getColor(R.color.colorRed));
}else{
isUsernameValidOrNot.setText("Kullanıcı adı harika görünüyor. Bence bunu kaçırmamalısın!");
isUsernameValidOrNot.setTextColor(getResources().getColor(R.color.colorGreen));
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
@Override
public void afterTextChanged(Editable editable) {
}
});
}
}
我尝试了下面的代码,但没有用。
Boolean isUsernameAlreadyExists;
//...
if(Objects.requireNonNull(dataSnapshot.getValue()).toString().equals(registerUsername.getText().toString())){
isUsernameValidOrNot.setText("Kullanıcı adı kullanımda. Başka bir tane demeneye ne dersin?");
isUsernameValidOrNot.setTextColor(getResources().getColor(R.color.colorRed));
isUsernameAlreadyExists = true;
}else{
isUsernameValidOrNot.setText("Kullanıcı adı harika görünüyor. Bence bunu kaçırmamalısın!");
isUsernameValidOrNot.setTextColor(getResources().getColor(R.color.colorGreen));
isUsernameAlreadyExists = false;
}
我还在结束循环之前尝试了方法外的布尔查询。没有用。
答案 0 :(得分:0)
由于orderByChild(charSequence.toString())
的这一部分而无法正常工作-这代表一个孩子,不是一个值,但您提供的是charSequence.toString()
,该值是由用户键入的。另外,为什么不尝试使用Query
却可以简化代码。例如:
Query query = databaseReference.child("users").orderByChild("username").equalTo(charSequence.toString());
// Note: Above provide your order of children this is just an example
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {...