Я создаю медицинское приложение, которое должно показать их краткий медицинский профиль и диаграмму этого.Для базы данных я использую Firebase в качестве BaaS.Мне нужна помощь, потому что моя диаграмма не может показать данные.Я использую Диаграмму Майка Фила, чтобы помочь мне показать мою диаграмму.Каждый раз, когда я помещаю код диаграммы в DataSnapshot, он не работает, но если я помещаю его вне DataSnapshot, он работает, но не может отображать данные из базы данных.Вот код:
public class ProfileActivity extends AppCompatActivity {
private static final int CHOOSE_IMAGE = 101;
private ImageView profilePic;
private TextView profileName, profileAge, profileEmail, profileHeight, profileWeight, profileListening, profileSights, profileHeadSize, profileBirthday, profileBirthMonth, profileBirthYear;
private Button profileUpdate, buttonSign;
private FirebaseAuth firebaseAuth;
private FirebaseDatabase firebaseDatabase;
private BarChart graphView;
private FirebaseAuth.AuthStateListener mAuthStateListener;
String profileImageUrl;
Uri uriProfileImage;
ProgressBar progressBar;
LineGraphSeries series;
ArrayList<ArrayList<String>> listString = new ArrayList<ArrayList<String>>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
profileName = findViewById(R.id.tvProfileName);
profileAge = findViewById(R.id.tvProfileAge);
buttonSign = findViewById(R.id.buttonSign);
profileUpdate = findViewById(R.id.btnProfileUpdate);
profileHeight = findViewById(R.id.tvProfileHeight);
profileWeight = findViewById(R.id.tvProfileWight);
profileListening = findViewById(R.id.tvProfileListening);
profileSights = findViewById(R.id.tvProfileSights);
profileHeadSize = findViewById(R.id.tvProfileHeadSize);
profileBirthday = findViewById(R.id.tvProfileBirthday);
progressBar = (ProgressBar) findViewById(R.id.progressbar);
firebaseAuth = FirebaseAuth.getInstance();
firebaseDatabase = FirebaseDatabase.getInstance();
loadUserInformation();
DatabaseReference databaseReference = firebaseDatabase.getReference(firebaseAuth.getUid());
DatabaseReference childReference = firebaseDatabase.getReference(databaseReference.getKey());
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
UserProfile userProfile = dataSnapshot.getValue(UserProfile.class);
profileName.setText("Name: " + userProfile.getUserName());
profileAge.setText("Age: " + userProfile.getUserAge() + " years old");
profileHeight.setText("Height: " + userProfile.getHeight() + " cm");
profileWeight.setText("Weight: " + userProfile.getWeight() + " kg");
profileHeadSize.setText("Head Circumference: " + userProfile.getHeadSize() + " cm");
profileListening.setText("Hearing: " + userProfile.getListening());
profileSights.setText("Eye Sight: " + userProfile.getSights());
profileBirthday.setText("Date of Birth: " + userProfile.getBirthday() + " / " + userProfile.getBirthdayMonth()+ " / " + userProfile.getBirthdayYear());
}
@Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(ProfileActivity.this, databaseError.getCode(), Toast.LENGTH_SHORT).show();
}
});
/** START OF GRAPH VIEW CODE **/
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
UserProfile userProfile = dataSnapshot.getValue(UserProfile.class);
int height = Integer.parseInt(userProfile.getHeight());
int weight = Integer.parseInt(userProfile.getWeight());
int headSize = Integer.parseInt(userProfile.getHeadSize());
graphView = (BarChart) findViewById(R.id.graphView);
graphView.setDrawBarShadow(false);
graphView.setDrawValueAboveBar(true);
graphView.setMaxVisibleValueCount(120);
graphView.setPinchZoom(false);
graphView.setDrawGridBackground(true);
ArrayList<BarEntry> barEntries = new ArrayList<>();
barEntries.add(new BarEntry(1, height));
barEntries.add(new BarEntry(2, weight));
barEntries.add(new BarEntry(3, headSize));
barEntries.add(new BarEntry(4, 36));
BarDataSet barDataSet = new BarDataSet(barEntries, "Data Set 1");
barDataSet.setColor(Color.RED);
BarData data = new BarData(barDataSet);
data.setBarWidth(0.4f);
graphView.setData(data);
String[] xaxisgraph = new String[] {"BBb", "TGb", "Pen", "Pel", "Paj"};
XAxis xAxis = graphView.getXAxis();
xAxis.setValueFormatter(new MyXAxisValueFormatter(xaxisgraph));
xAxis.setPosition(XAxis.XAxisPosition.BOTH_SIDED);
/** END OF GRAPH VIEW CODE **/
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
buttonSign.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
firebaseAuth.signOut();// optional depending on your needs
finish();
startActivity(new Intent(ProfileActivity.this, MainActivity.class));
}
});
profileUpdate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(ProfileActivity.this, UpdateProfileActivity.class));
}
});
}
public class MyXAxisValueFormatter implements IAxisValueFormatter {
private String[] mValues;
public MyXAxisValueFormatter(String[] values) {
this.mValues = values;
}
@Override
public String getFormattedValue(float value, AxisBase axis) {
return mValues[(int)value];
}
}
private void loadUserInformation() {
final FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
if (user.getPhotoUrl() != null) {
Glide.with(this)
.load(user.getPhotoUrl().toString())
.into(profilePic);
}
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CHOOSE_IMAGE && resultCode == RESULT_OK && data != null && data.getData() != null) {
uriProfileImage = data.getData();
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uriProfileImage);
profilePic.setImageBitmap(bitmap);
uploadImageToFirebaseStorage();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void uploadImageToFirebaseStorage() {
StorageReference profileImageRef =
FirebaseStorage.getInstance().getReference("profilepics/" + System.currentTimeMillis() + ".jpg");
if (uriProfileImage != null) {
progressBar.setVisibility(View.VISIBLE);
profileImageRef.putFile(uriProfileImage)
.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
progressBar.setVisibility(View.GONE);
profileImageUrl = taskSnapshot.getDownloadUrl().toString();
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
progressBar.setVisibility(View.GONE);
Toast.makeText(ProfileActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
}
@Override
public void onBackPressed()
{
// code here to show dialog
super.onBackPressed();// optional depending on your needs
finish();
startActivity(new Intent(ProfileActivity.this, MainActivity.class));
}
private void showImageChooser() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Profile Image"), CHOOSE_IMAGE);
}
}