我目前正在从事一个有助于记录用户资料的项目。我正在使用Firebase实时数据库作为移动应用程序的数据库。现在,问题是,我希望用户能够更新其数据(医学数据)并将其制成图表。因此,我想到了从第一个输入数据中将更新的数据添加为子数据的想法。子数据插入工作正常,但是在我的数据库中,它仅显示输入数据的一半。我需要帮助,以便将数据完全输入。
我的UpdateProfileActivity代码:
public class UpdateProfileActivity extends AppCompatActivity {
private EditText userName, userPassword, userEmail, userAge, userHeight, userWeight, userListening, userSights, userHeadsize, userBirthday;
private Button regButton;
private TextView userLogin;
private FirebaseAuth firebaseAuth;
private ImageView userProfilePic;
String email, name, age, password, height, weight, headsize, listening, sights, birthday, birthdaydate, birthdaymonth, birthdayyear;
private DatePickerDialog.OnDateSetListener mDateSetListener;
private static final String TAG = "UpdateProfileActivity";
String bddate, bdmonth, bdyear;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_update_profile);
setupUIViews();
firebaseAuth = FirebaseAuth.getInstance();
FirebaseUser user = firebaseAuth.getCurrentUser();
regButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//sendEmailVerification();
sendUserData();
Toast.makeText(UpdateProfileActivity.this, "Data Added!", Toast.LENGTH_SHORT);
//finish();
/*startActivity(new Intent(RegistrationActivity.this, MainActivity.class));*/
}
});
}
private void setupUIViews() {
userName = (EditText) findViewById(R.id.etUserName);
userEmail = (EditText) findViewById(R.id.etUserEmail);
regButton = (Button) findViewById(R.id.btnRegister);
userLogin = (TextView) findViewById(R.id.tvUserLogin);
userAge = (EditText) findViewById(R.id.etAge);
userHeadsize = (EditText) findViewById(R.id.etHeadSize);
userHeight = (EditText) findViewById(R.id.etHeight);
userWeight = (EditText) findViewById(R.id.etWeight);
userSights = (EditText)findViewById(R.id.etSights);
userListening = (EditText) findViewById(R.id.etListening);
userProfilePic = (ImageView) findViewById(R.id.ivProfile);
userBirthday = (EditText) findViewById(R.id.Birthday);
userBirthday.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH);
int day = cal.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dialog = new DatePickerDialog(
UpdateProfileActivity.this,
android.R.style.Theme_Holo_Light_Dialog_MinWidth,
mDateSetListener,
year,month,day);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.show();
}
});
mDateSetListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker datePicker, int year, int month, int day) {
month = month + 1;
Log.d(TAG, "onDateSet: mm/dd/yyy: " + day + "/" + month + "/" + year);
bddate = String.valueOf(day);
bdmonth = String.valueOf(month);
bdyear = String.valueOf(year);
userBirthday.setText(bddate + "/" + bdmonth+ "/" +bdyear);
}
};
}
private Boolean validate() {
Boolean result = false;
name = userName.getText().toString();
email = userEmail.getText().toString();
age = userAge.getText().toString();
height = userHeight.getText().toString();
weight = userWeight.getText().toString();
listening = userListening.getText().toString();
sights = userSights.getText().toString();
headsize = userHeadsize.getText().toString();
birthday = userBirthday.getText().toString();
if (name.isEmpty() || password.isEmpty() || email.isEmpty() || age.isEmpty() || height.isEmpty() || weight.isEmpty() || listening.isEmpty() || sights.isEmpty() || headsize.isEmpty() || birthday.isEmpty()) {
Toast.makeText(this, "Please enter all the details", Toast.LENGTH_SHORT).show();
} else {
result = true;
}
return result;
}
private void sendUserData() {
FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
DatabaseReference myRef = firebaseDatabase.getReference(firebaseAuth.getUid());
DatabaseReference childRef = firebaseDatabase.getReference(myRef.getKey());
UserProfile userProfile = new UserProfile(age, email, name, height, weight, headsize, listening, sights, bddate, bdmonth, bdyear);
childRef.push().setValue(userProfile);
}
}
我的UserProfile类代码:
public class UserProfile {
public String userAge;
public String userEmail;
public String userName;
public String userHeight, userWeight, userListening, userSights, userHeadsize, userBirthday, bdmonth, bdyear;
public UserProfile(){
}
public UserProfile(String userAge, String userEmail, String userName, String userheight, String userweight, String headsize, String listening, String sights, String birthday, String bdmonth, String bdyear) {
this.userAge = userAge;
this.userEmail = userEmail;
this.userName = userName;
this.userHeight = userheight;
this.userWeight = userweight;
this.userListening = listening;
this.userSights = sights;
this.userHeadsize = headsize;
this.userBirthday = birthday;
this.bdmonth = bdmonth;
this.bdyear = bdyear;
}
public String getUserAge() {
return userAge;
}
public void setUserAge(String userAge) {
this.userAge = userAge;
}
public String getUserEmail() {
return userEmail;
}
public void setUserEmail(String userEmail) {
this.userEmail = userEmail;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getHeight() {
return userHeight;
}
public String getWeight() {
return userWeight;
}
public String getHeadSize() {
return userHeadsize;
}
public String getListening() {
return userListening;
}
public String getSights() {
return userSights;
}
public String getBirthday() {
return userBirthday;
}
public String getBirthdayMonth() {
return bdmonth;
}
public String getBirthdayYear() {
return bdyear;
}
}
答案 0 :(得分:1)
您将必须将数据存储在Map对象中,然后将map对象推入子对象。
如下所示
请检查sendUserData()中的代码,我添加了新的代码行。
希望这会有所帮助。
public class UpdateProfileActivity extends AppCompatActivity {
private EditText userName, userPassword, userEmail, userAge, userHeight, userWeight, userListening, userSights, userHeadsize, userBirthday;
private Button regButton;
private TextView userLogin;
private FirebaseAuth firebaseAuth;
private ImageView userProfilePic;
String email, name, age, password, height, weight, headsize, listening, sights, birthday, birthdaydate, birthdaymonth, birthdayyear;
private DatePickerDialog.OnDateSetListener mDateSetListener;
private static final String TAG = "UpdateProfileActivity";
String bddate, bdmonth, bdyear;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_update_profile);
setupUIViews();
firebaseAuth = FirebaseAuth.getInstance();
FirebaseUser user = firebaseAuth.getCurrentUser();
regButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//sendEmailVerification();
sendUserData();
Toast.makeText(UpdateProfileActivity.this, "Data Added!", Toast.LENGTH_SHORT);
//finish();
/*startActivity(new Intent(RegistrationActivity.this, MainActivity.class));*/
}
});
}
private void setupUIViews() {
userName = (EditText) findViewById(R.id.etUserName);
userEmail = (EditText) findViewById(R.id.etUserEmail);
regButton = (Button) findViewById(R.id.btnRegister);
userLogin = (TextView) findViewById(R.id.tvUserLogin);
userAge = (EditText) findViewById(R.id.etAge);
userHeadsize = (EditText) findViewById(R.id.etHeadSize);
userHeight = (EditText) findViewById(R.id.etHeight);
userWeight = (EditText) findViewById(R.id.etWeight);
userSights = (EditText)findViewById(R.id.etSights);
userListening = (EditText) findViewById(R.id.etListening);
userProfilePic = (ImageView) findViewById(R.id.ivProfile);
userBirthday = (EditText) findViewById(R.id.Birthday);
userBirthday.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH);
int day = cal.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dialog = new DatePickerDialog(
UpdateProfileActivity.this,
android.R.style.Theme_Holo_Light_Dialog_MinWidth,
mDateSetListener,
year,month,day);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.show();
}
});
mDateSetListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker datePicker, int year, int month, int day) {
month = month + 1;
Log.d(TAG, "onDateSet: mm/dd/yyy: " + day + "/" + month + "/" + year);
bddate = String.valueOf(day);
bdmonth = String.valueOf(month);
bdyear = String.valueOf(year);
userBirthday.setText(bddate + "/" + bdmonth+ "/" +bdyear);
}
};
}
private Boolean validate() {
Boolean result = false;
name = userName.getText().toString();
email = userEmail.getText().toString();
age = userAge.getText().toString();
height = userHeight.getText().toString();
weight = userWeight.getText().toString();
listening = userListening.getText().toString();
sights = userSights.getText().toString();
headsize = userHeadsize.getText().toString();
birthday = userBirthday.getText().toString();
if (name.isEmpty() || password.isEmpty() || email.isEmpty() || age.isEmpty() || height.isEmpty() || weight.isEmpty() || listening.isEmpty() || sights.isEmpty() || headsize.isEmpty() || birthday.isEmpty()) {
Toast.makeText(this, "Please enter all the details", Toast.LENGTH_SHORT).show();
} else {
result = true;
}
return result;
}
private void sendUserData() {
FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
DatabaseReference myRef = firebaseDatabase.getReference(firebaseAuth.getUid());
DatabaseReference childRef = firebaseDatabase.getReference(myRef.getKey());
Map<String, User> users = new HashMap<>();
users.put(firebaseAuth.getUid(), new UserProfile(age, email, name, height, weight, headsize, listening, sights, bddate, bdmonth, bdyear));
// UserProfile userProfile = new UserProfile(age, email, name, height, weight, headsize, listening, sights, bddate, bdmonth, bdyear);
childRef.push().setValue(users);
}
}
答案 1 :(得分:0)
使用 DatabaseReference.CompletionListener
childRef.push().setValue(userProfile,new DatabaseReference.CompletionListener() {
@Override
public void onComplete(DatabaseError databaseError, DatabaseReference reference) {
if (databaseError != null) {
Log.e(TAG, "Failed to write message", databaseError.toException());
}
}
});