我正在创建一个Android应用来存储每个用户的姓名和手机号码,现在当第二个用户更新其信息时,它将删除存储在数据库中的第一个用户的信息,我有2个片段,其中一个是登录片段(使用firebase auth)和第二个获取名称和手机号码的片段。我如何分别为每个用户添加信息
这是我的登录代码
btn_signIn = (Button) view.findViewById(R.id.sign_in_btn);
btn_signIn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String emailID = username.getText().toString();
String paswd = password.getText().toString();
if (emailID.isEmpty()) {
username.setError("Provide your Email first!");
username.requestFocus();
} else if (paswd.isEmpty()) {
password.setError("Set your password");
password.requestFocus();
}
else if (emailID.isEmpty() && paswd.isEmpty()) {
Toast.makeText(getActivity(), "Fields Empty!", Toast.LENGTH_SHORT).show();
}
else if (!(emailID.isEmpty() && paswd.isEmpty())) {
mAuth.signInWithEmailAndPassword(emailID, paswd)
.addOnCompleteListener(getActivity(), new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
// If sign in fails, display a message to the user. If sign in succeeds
// the auth state listener will be notified and logic to handle the
// signed in user can be handled in the listener.
if (!task.isSuccessful()) {
// there was an error
if (password.length() < 6) {
// inputPassword.setError(getString(R.string.minimum_password));
} else {
// Toast.makeText(LoginActivity.this, getString(R.string.auth_failed), Toast.LENGTH_LONG).show();
}
} else {
Intent intent = new Intent(getActivity(), HomePageActivity.class);
startActivity(intent);
// finish();
}
}
});
。
}
}
});
这是我的代码,用于将手机号码存储在另一个片段的Firebase中
new AlertDialog.Builder(getActivity()).setTitle("Please Input Contact Information").setIcon(
android.R.drawable.ic_dialog_dialer).setView(
layout).setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Dialog dialog = (Dialog) dialogInterface;
EditText inputMobile = (EditText) dialog.findViewById(R.id.dialog_et_mobile);
if (inputMobile.getText().toString().isEmpty()){
return;
}
try{
long number = Long.valueOf(inputMobile.getText().toString());
SPManipulation.getInstance(getActivity()).setMobile(inputMobile.getText().toString());
mTextMobile.setText(inputMobile.getText().toString());
String mobile = inputMobile.getText().toString();
DatabaseReference mynum = database.getReference("number");
}catch (Exception e){
Toast.makeText(getActivity(), "Please Input Correct Phone Number!", Toast.LENGTH_SHORT).show();
}
}
}).setNegativeButton("Cancel", null).show();
}
});
谁能帮助分别存储每个用户的信息,我进行了很多搜索,但没有任何帮助。可能是因为我非常新手..请帮助我
答案 0 :(得分:1)
尝试一下,
new AlertDialog.Builder(getActivity()).setTitle("Please Input Contact Information").setIcon(
android.R.drawable.ic_dialog_dialer).setView(
layout).setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Dialog dialog = (Dialog) dialogInterface;
EditText inputMobile = (EditText) dialog.findViewById(R.id.dialog_et_mobile);
if (inputMobile.getText().toString().isEmpty()){
return;
}
try{
long number = Long.valueOf(inputMobile.getText().toString());
SPManipulation.getInstance(getActivity()).setMobile(inputMobile.getText().toString());
mTextMobile.setText(inputMobile.getText().toString());
FirebaseAuth mAuth = FirebaseAuth.getInstance();
String userID = mAuth.getCurrentUser().getUid();
String mobile = inputMobile.getText().toString();
DatabaseReference mynum = database.getReference().child(userID).child("number");
}catch (Exception e){
Toast.makeText(getActivity(), "Please Input Correct Phone Number!", Toast.LENGTH_SHORT).show();
}
}
}).setNegativeButton("Cancel", null).show();
}
});
希望对您有充分的帮助
答案 1 :(得分:1)
尝试以下代码 使用以下代码添加产品
DatabaseReference rootRef;
rootRef = FirebaseDatabase.getInstance().getReference();
CommonLocationClass dataLocation = new CommonLocationClass();
dataLocation.setLat(latLng.latitude);
dataLocation.setLongi(latLng.longitude);
dataLocation.setPhno("");
dataLocation.setStatus(true);
if (sessionManager.phNO() != null) {
rootRef.child(sessionManager.phNO()).setValue(dataLocation);
}
commonlocationclass是具有getter和setter的模型类 .child(sessionManager.phNo())是子名称,您可以通过该子名称进一步标识项目
通过此代码,您可以更新字段
Map<String, Object> map = new HashMap<>();
map.put("lat", latLng.latitude);
map.put("longi", latLng.longitude);
map.put("phno", sessionManager.phNO());
map.put("status", true);
if (sessionManager.phNO() != null)
rootRef.child(sessionManager.phNO()).updateChildren(map);
尝试此代码,希望它能帮助您快乐编码!!!!!!