问题
Here I am trying to display the data for the current logged in user. If there is any other method to retrieve data pls share.
This is my database :
This is the code I have used for saving data :
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().hide();
setContentView(R.layout.activity_signup);
mfirebase = FirebaseAuth.getInstance();
msuser=(EditText)findViewById(R.id.signup_username);
mspass=(EditText)findViewById(R.id.signup_password);
mloginn=(TextView)findViewById(R.id.already);
msconfpass=(EditText)findViewById(R.id.confirm_password);
uname=(EditText)findViewById(R.id.uname);
ugender=(EditText)findViewById(R.id.ugender);
uheight=(EditText)findViewById(R.id.uheight);
uweight=(EditText)findViewById(R.id.uweight);
ud= new UserDetails();
reference=FirebaseDatabase.getInstance().getReference().child("User");
msave=(Button)findViewById(R.id.save);
msave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//n
String uName= uname.getText().toString().trim();
Float uHeight = Float.parseFloat(uheight.getText().toString().trim());
Float uWeight = Float.parseFloat(uweight.getText().toString().trim());
String uGender= ugender.getText().toString().trim();
String uEmail= msuser.getText().toString().trim();
ud.setName(uName);
ud.setHeight(uHeight);
ud.setWeight(uWeight);
ud.setGender(uGender);
ud.setEmail(uEmail);
reference.push().setValue(ud);
//n
email= msuser.getText().toString();
String pwd=mspass.getText().toString();
String cpwd=msconfpass.getText().toString();
if(email.isEmpty()){
msuser.setError("Please enter email id");
msuser.requestFocus();
}
else if(pwd.isEmpty()){
mspass.setError("Please enter the password");
mspass.requestFocus();
}
else if(cpwd.isEmpty()){
msconfpass.setError("Please enter password again");
msconfpass.requestFocus();
}
else if(email.isEmpty() && pwd.isEmpty() && cpwd.isEmpty()){
Toast.makeText(Signup.this,"Fields are empty",Toast.LENGTH_SHORT).show();
}
else if(!cpwd.equals(pwd)){
Toast.makeText(Signup.this,"Please enter same password",Toast.LENGTH_SHORT).show();
}
else if(!(email.isEmpty() && pwd.isEmpty() && cpwd.isEmpty())){
mfirebase.createUserWithEmailAndPassword(email,pwd).addOnCompleteListener(Signup.this, new OnCompleteListener<com.google.firebase.auth.AuthResult>() {
@Override
public void onComplete(@NonNull Task<com.google.firebase.auth.AuthResult> task) {
if(!task.isSuccessful()){
Toast.makeText(Signup.this,"Signup Unsuccessful",Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(Signup.this,"Account Created Successfully and You're now logged in",Toast.LENGTH_SHORT).show();
finish();
startActivity(new Intent(Signup.this,Login.class));
//Toast.makeText(Signup.this,"Account Created Successfully",Toast.LENGTH_SHORT).show();
}
}
});
}
else{
Toast.makeText(Signup.this,"Error",Toast.LENGTH_SHORT).show();
}
}
});
This is the code I have used for retrieving the data :
showb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//reference= (DatabaseReference) FirebaseDatabase.getInstance().getReference().child("User").orderByChild("email").equalTo(strEmailId);
reference= FirebaseDatabase.getInstance().getReference().child("User").child(FirebaseAuth.getInstance().getCurrentUser().getUid());
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
String namep=dataSnapshot.child("name").getValue(String.class);
String heightp=dataSnapshot.child("height").getValue(String.class);
String weightp=dataSnapshot.child("weight").getValue(String.class);
String genderp=dataSnapshot.child("gender").getValue(String.class);
String emailp=dataSnapshot.child("email").getValue(String.class);
name.setText(namep);
height.setText(heightp);
weight.setText(weightp);
gender.setText(genderp);
mail.setText(emailp);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
});
}
This is the error shown after clicking on showb (Button used for displaying in text fields): After clicking on showb the error is as follows :
2020-04-01 19:28:18.285 7996-7996/com.zzz.fitness D/ViewRootImpl@133c623[UserProfile]: ViewPostIme pointer 0
2020-04-01 19:28:18.359 7996-7996/com.zz.fitness D/ViewRootImpl@133c623[UserProfile]: ViewPostIme pointer 1
回答1:
You are storing the values under a random id:
reference.push().setValue(ud);
This code generates a random id and under it you will have the attributes of class UserDetails, you need to change it to the following:
mfirebase.createUserWithEmailAndPassword(email,pwd).addOnCompleteListener(Signup.this, new OnCompleteListener<com.google.firebase.auth.AuthResult>() {
@Override
public void onComplete(@NonNull Task<com.google.firebase.auth.AuthResult> task) {
if(!task.isSuccessful()){
Toast.makeText(Signup.this,"Signup Unsuccessful",Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(Signup.this,"Account Created Successfully and You're now logged in",Toast.LENGTH_SHORT).show();
reference.child(task.getResult().getUser().getUid()).setValue(ud); //add this line
finish();
startActivity(new Intent(Signup.this,Login.class));
//Toast.makeText(Signup.this,"Account Created Successfully",Toast.LENGTH_SHORT).show();
}
After you change it to the above, then when you retrieve you can use this:
reference= FirebaseDatabase.getInstance().getReference().child("User").child(FirebaseAuth.getInstance().getCurrentUser().getUid());
来源:https://stackoverflow.com/questions/60972719/view-postime-0-1-error-while-retrieving-data-from-firebase