android - Firebase 图表可视化无法显示 Android
问题描述
我正在创建一个医疗应用程序,该应用程序需要显示他们的简短医疗概况和图表。对于数据库,我使用 Firebase 作为 BaaS。我需要帮助,因为我的图表无法显示数据。我正在使用 Mike Phil 的图表来帮助我展示我的图表。每次我将图表代码放入 DataSnapshot 时,它都不起作用,但如果我将它放在 DataSnapshot 之外,它会起作用,但它无法显示来自 firebase 的数据。这是代码:
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);
}
}
解决方案
解决方案是在函数中创建图形代码,然后将其插入数据快照中。
推荐阅读
- javascript - 如何在vue中将表单提交到数组并清除输入
- python - 使用mss时如何重置屏幕坐标
- r - r 使用 lat lon 而不是 fips 代码将气泡和等值线图巧妙地组合在一起
- python - 如何在opencv中围绕来自keras模型的火灾检测对象绘制矩形
- android - 我在哪里可以在云端创建一个可以实时更新的 HashMap?
- jsonata - 使用 Transform 运算符从对象中删除一个或多个键/值对?
- python - 使用多索引数据框时如何在 seaborn clustermap 中排列 y 标签?
- database - 设计一个用户可以访问任务的系统
- regex - 如何包围以 ! 开头的文本 直到 sed 中的转义字符结束?
- google-assistant-sdk - 谷歌助理自动播放的可能性