您好我想要一个建议如何在云中保存条形图(MPChartAndroid库)数据我使用Gson到Json然后我将ArrayList写入FireBase和共享首选项它是正确的方法吗?
问题是:当我加载数据时,前两个条目设置为0 例如:我从2016年7月1日到2012年7月1日有一个每日BarChart。 我在28.06.2017到01.07.2017注册数据,但是第二天02.07.2017我在应用程序中输入,28.06和29.06的条目设置为0.
此外,我希望if用户当前日期高于日期ArrayList中的最后一个日期,以添加新的日期集,但保留旧的日期,并保留其中的dataEntry。
这是ArrayLists的声明
private ArrayList<BarEntry> barEntryArrayList = new ArrayList<>();
private ArrayList<String> datelist = new ArrayList<>();
从共享首选项中检索数据
SharedPreferences sharedPreferences = getSharedPreferences("SharedPrefs", 0);
SharedPreferences sharedChronoPrefs = getSharedPreferences("ChronometerSample", MODE_PRIVATE);
Type type = new TypeToken<ArrayList<String>>() {}.getType();
Type type2 = new TypeToken<ArrayList<BarEntry>>() {}.getType();
String jsonResp = sharedPreferences.getString("jsonString", null);
if (jsonResp != null) {
datelist = gson.fromJson(jsonResp, type);
}
Log.d(TAG, "loadDataList() called");
String json = sharedPreferences.getString("json", null);
if (json != null) {
barEntryArrayList = gson.fromJson(json, type2);
}
设置图表
addPreviousData();
addNewEntry();
BarDataSet barDataSet = new BarDataSet(barEntryArrayList, "# of Questions Answered");
LocalDate todayD = new LocalDate();
String tdy = todayD.toString();
BarData barData = new BarData(datelist, barDataSet);
int posX = datelist.indexOf(tdy);
currentPos = barData.getXValCount() - posX;
mChart.setScaleMinima((float) barData.getXValCount() / 5f, 1f);
mChart.zoom(-10f, 0f, 0, 0);
mChart.moveViewToX(barData.getXValCount() - currentPos - 3);
mChart.invalidate();
barDataSet.setValueTextColor(Color.GREEN);
barDataSet.setValueTextSize(14f);
barDataSet.setColor(Color.GREEN);
mChart.setData(barData);
mChart.notifyDataSetChanged();
mChart.animateXY(2000, 1000);
YAxis leftAxis = mChart.getAxisLeft();
leftAxis.setTextColor(Color.WHITE);
leftAxis.setAxisMinValue(0f);
leftAxis.setAxisMaxValue(100f);
将日期添加到ArrayList日期列表
private void addPreviousData() {
if (datelist == null || datelist.size() <= 0) {
LocalDate endDate = new LocalDate();
endDate = endDate.plusYears(1);
LocalDate starDate = endDate.minusYears(2);
List<LocalDate> totalDates = new ArrayList<>();
while (!starDate.isAfter(endDate)) {
totalDates.add(starDate);
starDate = starDate.plusDays(1);
}
for (int i = 0; i < totalDates.size(); i++) {
datelist.add(totalDates.get(i).toString());
}
Log.d(TAG, "addPreviousData() called" + totalDates.size());
Log.d(TAG, "addPreviousData :" + datelist.size());
}
}
在图表中添加条目
private void addNewEntry() {
if (barEntryArrayList == null || barEntryArrayList.size() <= 0) {
for (int i = 0; i < datelist.size(); i++) {
barEntryArrayList.add(new BarEntry(0f, i));
}
mChart.notifyDataSetChanged();
mChart.invalidate();
Log.d(TAG, "addNewEntry() returned: " + barEntryArrayList.size());
}
}
更新当前日期位置:
// int posX = datelist.indexOf(tdy);
// currentPos = barData.getXValCount() - posX;
private void updateEntry(float updateValue) {
int updatePos = datelist.size() - currentPos;
if (isNewDay()) {
updatePos++;
}
BarEntry barEntry = barEntryArrayList.get(updatePos);
Log.d(TAG, "updateEntry() returned: position " + datelist.get(datelist.size() - currentPos));
barEntry.setVal(updateValue);
mChart.notifyDataSetChanged();
mChart.invalidate();
}
保存数据
private void saveDataList() {
Gson gson = new Gson();
SharedPreferences sharedPreferences = getSharedPreferences("SharedPrefs", 0);
SharedPreferences.Editor editor = sharedPreferences.edit();
String jsonString = gson.toJson(datelist);
String json = gson.toJson(barEntryArrayList);
editor.putString("jsonString", jsonString).apply();
editor.putString("json", json).apply();
LocalDate tomorrow = new LocalDate().plusDays(1);
editor.putString("LocalTime", tomorrow.toString());
}
检查是否是新的一天
private boolean isNewDay() {
SharedPreferences sharedPreferences = getSharedPreferences("SharedPrefs", 0);
boolean isNewDay;
LocalDate todayDate = new LocalDate();
String tomorrow = sharedPreferences.getString("LocalTime", todayDate.plusDays(1).toString());
Log.d(TAG, "checkDates: sharedpRefs Tomorrow : " + sharedPreferences.getString("LocalTime", null));
String today = todayDate.toString();
if (today.equals(tomorrow)) {
tomorrow = todayDate.plusDays(1).toString();
isNewDay = true;
Log.d(TAG, "checkDates: Tomorow at End if/else :value : " + isNewDay + " " + tomorrow);
} else {
isNewDay = false;
Log.d(TAG, "checkDates: Tomorow at End if/else :value : " + isNewDay + " " + tomorrow);
}
return isNewDay;
}
和onCreate()
if (isNewDay() || isFirstRun) {
dailyQuestion = 0;
}
答案 0 :(得分:0)
使用Gson库我将BarEntry的ArrayList转换为JSON String,将值保存在Firese中,然后在检索时简单地转换为BarEntry List。计算ArrayList中项目的位置,并根据MPAndroidChart库的文档更新值