gpt4 book ai didi

java - 如何使用 MPAndroidChart 库将 float 数据从 firebase 转换为图表的日期数据

转载 作者:行者123 更新时间:2023-12-01 17:26:01 24 4
gpt4 key购买 nike

我是 Android 开发新手,在将 float 数据从 Firebase Firestore 转换为最新版本时遇到问题。我希望使用 MPAndroidChart 库将此日期数据显示在图表中。自定义类有体重日期两个变量。我希望用户在可编辑文本中写入自己的当前体重,并在按下按钮后将体重和当前日期保存在 Firebase Firestore 数据库中。然后我希望将保存的数据显示在折线图中。

正如你在我的数据库截图中看到的Firebase Firestore float 中有日期变量。主要问题是我不知道如何将该浮点变量转换为 SimpleDataFormat (dd/MM)

这是 fragment

public class ReportFragment extends Fragment {

private ArrayList<Entry> weightDateList;
private LineChart mWeightChart;
private Button btnWeight;
private EditText inputWeight;
private FirebaseFirestore db = FirebaseFirestore.getInstance();
private static final String TAG = "ReportFragment";
private Date today = new Date(System.currentTimeMillis());
private final SimpleDateFormat mFormat = new SimpleDateFormat("dd/MM");


public ReportFragment() {}

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_report, container, false);
mWeightChart = view.findViewById(R.id.weight_chart);
btnWeight = view.findViewById(R.id.add_weight);
inputWeight = view.findViewById(R.id.input_weight);

XAxis xWeightAxis = mWeightChart.getXAxis();
xWeightAxis.setValueFormatter(new ValueFormatter() {
@Override
public String getFormattedValue(float dateWeight) {
return mFormat.format(dateWeight);
}
});

db.collection("WeightChartData").addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable FirebaseFirestoreException e) {
if(e != null) {
Log.d(TAG, "onWeightEvent: " + e.getMessage());
return;
}
weightDateList = new ArrayList<Entry>();
for(QueryDocumentSnapshot weightDoc : queryDocumentSnapshots) {
if(weightDoc.exists()) {
WeightDateData weightData = weightDoc.toObject(WeightDateData.class);
weightDateList.add(new Entry(weightData.getWeight(), weightData.getDateWeight()));
}
}
}
});

LineDataSet weightDataSet = new LineDataSet(weightDateList, "WeightGraph");
LineData weightData = new LineData(weightDataSet);
mWeightChart.setData(weightData);

btnWeight.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
saveWeight();
}
});
return view;
}

public void saveWeight() {
float weight = Float.valueOf(inputWeight.getText().toString());
float dateWeight = Float.valueOf(today.getTime());
WeightDateData weightData = new WeightDateData(weight, dateWeight);
db.collection("WeightChartData").document().set(weightData).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Toast.makeText(getActivity(), "Input weight was success", Toast.LENGTH_SHORT).show();
}
});
}
}

我希望这个示例能够帮助解决这个问题。谢谢大家!

最佳答案

我没有尝试这段代码。如果有错误你就改正它。

ArrayList<WeightDateData> list = new ArrayList<WeightDateData>();
XAxis xWeightAxis = mWeightChart.getXAxis();
xWeightAxis.setValueFormatter(new ValueFormatter() {
@Override
public String getFormattedValue(float value) {
if(value < 0 || value > list.size() -1)
return "";
return mFormat.format(list.get((int)value).getDateWeight());
}
});

int i = -1;
db.collection("WeightChartData").addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable FirebaseFirestoreException e) {
if(e != null) {
Log.d(TAG, "onWeightEvent: " + e.getMessage());
return;
}
weightDateList = new ArrayList<Entry>();
for(QueryDocumentSnapshot weightDoc : queryDocumentSnapshots) {
if(weightDoc.exists()) {
WeightDateData weightData = weightDoc.toObject(WeightDateData.class);
list.add(weightData)
i++;
weightDateList.add(new Entry(i, weightData.getWeight()));
}
}
LineDataSet weightDataSet = new LineDataSet(weightDateList, "WeightGraph");
LineData weightData = new LineData(weightDataSet);
mWeightChart.setData(weightData);
}
});

关于java - 如何使用 MPAndroidChart 库将 float 数据从 firebase 转换为图表的日期数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61210081/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com