MPAndroidChart 条目无法转换为 BarEntry
MPAndroidChart Entry cannot be cast to BarEntry
我想使用 MPAndroidChart 创建条形图,但出现以下错误 java.lang.ClassCastException: com.github.mikephil.charting.data.Entry cannot be cast to com.github.mikephil.charting.data.BarEntry
我首先从另一个 activity 获取数据并使用它。
第一个activity:
ArrayList<BarEntry> temperature = new ArrayList<>();
for (int i = 0; i < temp.length(); i++) {
float temp_data = Float.parseFloat(temp.getJSONObject(i).getString("value"));
float humid_data = Float.parseFloat(humid.getJSONObject(i).getString("value"));
float time_data = Float.parseFloat(time.getJSONObject(i).getString("time"));
temperature.add(new BarEntry(time_data, temp_data));
humidity.add(new BarEntry(time_data, humid_data));
}
Intent intent = new Intent(itemView.getContext(), DeviceDataReport.class);
intent.putExtra("temperatureData", temperature);
//put other extras
context.startActivity(intent);
在设备数据报告中:
ArrayList<BarEntry> temperature = new ArrayList<>();
if (extras != null) {
temperature = extras.getParcelableArrayList("temperatureData");
//get other data
}
temperatureChart = (BarChart) findViewById(R.id.chart_temp);
BarDataSet set1;
if (temperatureChart.getData() != null &&
temperatureChart.getData().getDataSetCount() > 0) {
set1 = (BarDataSet) temperatureChart.getData().getDataSetByIndex(0);
set1.setValues(temperature);
temperatureChart.getData().notifyDataChanged();
temperatureChart.notifyDataSetChanged();
} else {
set1 = new BarDataSet(temperature, "The year 2017"); //this is where error occurs
ArrayList<IBarDataSet> dataSets = new ArrayList<IBarDataSet>();
dataSets.add(set1);
BarData data = new BarData(dataSets);
temperatureChart.setData(data);
}
我没有看到任何我只使用 Entry
而不是 BarEntry
的地方。我的 xml 也说 BarChart 而不是 LineChart。
使用 MPAndroidChart 3.0.1
很抱歉,这看起来像是 BarEntry
的不完整实施。如果您检查源代码,您可以看到以下内容:
BarEntry
扩展 Entry
Entry
实施 Parcelable
BarEntry
没有自己的 Parcelable<Creator>
所以当你序列化成一个包裹,然后反序列化时,你会得到一个 List<Entry>
而不是 List<BarEntry>
:-)
目前,除了解决此问题外,您无能为力。
您可以借此机会重构以实现更好的层分离。 BarEntry
是视图层或视图模型层的成员。你应该有一个清晰的模型层,而不是传递 BarEntry
,它只是一个数据对象。您可以轻松地在 Intent 中传递此模型层的列表或数组,并且消费者可以根据需要转换为 BarEntry
。
或者,创建数据源的抽象。像 TemperatureRepository
class 这样的东西。然后使用这些数据的 Activity 和 Fragments 可以从存储库而不是从 Intent 中获取数据。
我想使用 MPAndroidChart 创建条形图,但出现以下错误 java.lang.ClassCastException: com.github.mikephil.charting.data.Entry cannot be cast to com.github.mikephil.charting.data.BarEntry
我首先从另一个 activity 获取数据并使用它。
第一个activity:
ArrayList<BarEntry> temperature = new ArrayList<>();
for (int i = 0; i < temp.length(); i++) {
float temp_data = Float.parseFloat(temp.getJSONObject(i).getString("value"));
float humid_data = Float.parseFloat(humid.getJSONObject(i).getString("value"));
float time_data = Float.parseFloat(time.getJSONObject(i).getString("time"));
temperature.add(new BarEntry(time_data, temp_data));
humidity.add(new BarEntry(time_data, humid_data));
}
Intent intent = new Intent(itemView.getContext(), DeviceDataReport.class);
intent.putExtra("temperatureData", temperature);
//put other extras
context.startActivity(intent);
在设备数据报告中:
ArrayList<BarEntry> temperature = new ArrayList<>();
if (extras != null) {
temperature = extras.getParcelableArrayList("temperatureData");
//get other data
}
temperatureChart = (BarChart) findViewById(R.id.chart_temp);
BarDataSet set1;
if (temperatureChart.getData() != null &&
temperatureChart.getData().getDataSetCount() > 0) {
set1 = (BarDataSet) temperatureChart.getData().getDataSetByIndex(0);
set1.setValues(temperature);
temperatureChart.getData().notifyDataChanged();
temperatureChart.notifyDataSetChanged();
} else {
set1 = new BarDataSet(temperature, "The year 2017"); //this is where error occurs
ArrayList<IBarDataSet> dataSets = new ArrayList<IBarDataSet>();
dataSets.add(set1);
BarData data = new BarData(dataSets);
temperatureChart.setData(data);
}
我没有看到任何我只使用 Entry
而不是 BarEntry
的地方。我的 xml 也说 BarChart 而不是 LineChart。
使用 MPAndroidChart 3.0.1
很抱歉,这看起来像是 BarEntry
的不完整实施。如果您检查源代码,您可以看到以下内容:
BarEntry
扩展Entry
Entry
实施Parcelable
BarEntry
没有自己的Parcelable<Creator>
所以当你序列化成一个包裹,然后反序列化时,你会得到一个 List<Entry>
而不是 List<BarEntry>
:-)
目前,除了解决此问题外,您无能为力。
您可以借此机会重构以实现更好的层分离。 BarEntry
是视图层或视图模型层的成员。你应该有一个清晰的模型层,而不是传递 BarEntry
,它只是一个数据对象。您可以轻松地在 Intent 中传递此模型层的列表或数组,并且消费者可以根据需要转换为 BarEntry
。
或者,创建数据源的抽象。像 TemperatureRepository
class 这样的东西。然后使用这些数据的 Activity 和 Fragments 可以从存储库而不是从 Intent 中获取数据。