我正在编写一个Android应用程序,我想检查是否存在字符串(如下面的userCPF和userCRM)以避免重复值。我一直在调查,但看起来我可以添加的只是听众,当我只是想检查一个ID是否存在时。
问题
我该怎么做?
以下是我的ProfileActivity.java和UserInformation(数据库格式)类。
首先,ProfileActivity:
public class ProfileActivity extends AppCompatActivity implements View.OnClickListener {
private FirebaseAuth mAuth;
private TextView textView;
private Button buttonLogout;
private DatabaseReference databaseReference;
private EditText editTextFName, editTextLName, editTextEmail, editTextCPF, editTextCRM;
private Spinner spinnerState;
private Button buttonSave;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
mAuth = FirebaseAuth.getInstance();
//if user is not logged in
if (mAuth.getCurrentUser() == null) {
finish();
startActivity(new Intent(this, MainActivity.class));
}
databaseReference = FirebaseDatabase.getInstance().getReference();
//initializing views
textView = (TextView) findViewById(R.id.textView);
editTextFName = (EditText) findViewById(R.id.editTextFName);
editTextLName = (EditText) findViewById(R.id.editTextLName);
editTextEmail = (EditText) findViewById(R.id.editTextEmail);
editTextCPF = (EditText) findViewById(R.id.editTextCPF);
editTextCRM = (EditText) findViewById(R.id.editTextCRM);
spinnerState = (Spinner) findViewById(R.id.spinnerState);
buttonSave = (Button) findViewById(R.id.buttonSave);
buttonLogout = (Button) findViewById(R.id.buttonLogout);
FirebaseUser user = mAuth.getCurrentUser();
textView.setText("Bem Vindo(a) Dr. " + user.getEmail());
editTextEmail.setText(user.getEmail());
//adding listener to buttons
buttonLogout.setOnClickListener(this);
buttonSave.setOnClickListener(this);
}
private void saveUserInformation() {
String firstName = editTextFName.getText().toString().trim();
String lastName = editTextLName.getText().toString().trim();
String userEmail = editTextEmail.getText().toString();
String userCPF = editTextCPF.getText().toString().trim();
String userCRM = editTextCRM.getText().toString().trim();
String crmState = spinnerState.getSelectedItem().toString();
//checking if email and passwords are empty
if(TextUtils.isEmpty(firstName)){
editTextFName.setError("Por favor, digite seu primeiro nome");
editTextFName.requestFocus();
return;
}
if(TextUtils.isEmpty(lastName)){
editTextLName.setError("Por favor, digite seu sobrenome");
editTextLName.requestFocus();
return;
}
if(TextUtils.isEmpty(userEmail)){
editTextEmail.setError("Por favor, digite seu e-mail");
editTextEmail.requestFocus();
return;
}
if(TextUtils.isEmpty(userCPF)){
editTextCPF.setError("Por favor, digite seu CPF");
editTextCPF.requestFocus();
return;
}
if(TextUtils.isEmpty(userCRM)){
editTextCRM.setError("Por favor, digite seu CRM");
editTextCRM.requestFocus();
return;
}
UserInformation userInformation = new UserInformation(firstName, lastName, userEmail, userCPF, crmState, userCRM);
FirebaseUser user = mAuth.getCurrentUser();
databaseReference.child(user.getUid()).setValue(userInformation);
Toast.makeText(this, "Dados Salvos...", Toast.LENGTH_LONG).show();
finish();
startActivity(new Intent(this, BadgeActivity.class));
}
@Override
public void onClick(View view) {
//if logout button is pressed
if (view == buttonLogout) {
mAuth.signOut();
finish();
startActivity(new Intent(this, MainActivity.class));
}
if (view == buttonSave) {
saveUserInformation();
}
}
然后,UserInformation
public class UserInformation {
public String firstName;
public String lastName;
public String userEmail;
public String crmState;
public String userCRM;
public String userCPF;
public UserInformation(){
}
public UserInformation(String firstName, String lastName, String userEmail, String userCPF, String crmState, String userCRM) {
this.firstName = firstName;
this.lastName = lastName;
this.crmState = crmState;
this.userCRM = userCRM;
this.userCPF = userCPF;
this.userEmail = userEmail;
}
答案 0 :(得分:1)
试试这个:
DatabaseReference ref=FirebaseDatabase.getInstance().getReference().child("UserInformation");
ref.orderByChild("userCRM").equalTo(CRM_value_here).addValueEventListener(new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot dataSnapshot){
if(dataSnapshot.exists()){
Toast.makeText(Activity_Name.this, "Stop!",Toast.LENGTH_LONG).show();
return;
}
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
可以执行上述操作,或者如果您使用用户的ID作为密钥,则可以执行此操作:
FirebaseUser currentuser=FirebaseAuth.getInstance().getCurrentUser();
ref.orderByKey().equalTo(currentuser.getUid()).addValueEventListener(new ValueEventListener(){
//like above
}