无法将MPAndroidChart条目强制转换为BarEntry [英] MPAndroidChart Entry cannot be cast to BarEntry

查看:290
本文介绍了无法将MPAndroidChart条目强制转换为BarEntry的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用MPAndroidChart创建BarChart,但出现以下错误java.lang.ClassCastException: com.github.mikephil.charting.data.Entry cannot be cast to com.github.mikephil.charting.data.BarEntry

I want to create a BarChart using MPAndroidChart but I get the following error java.lang.ClassCastException: com.github.mikephil.charting.data.Entry cannot be cast to com.github.mikephil.charting.data.BarEntry

我首先从其他活动中获取数据,然后使用它.

I first get the data from a different activity and use that.

第一次活动:

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);

在DeviceDataReport中:

In DeviceDataReport:

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.

I don't see any place where I am only using Entry instead of BarEntry. My xml also says BarChart not LineChart.

推荐答案

使用MPAndroidChart 3.0.1

很抱歉,这似乎是BarEntry的不完整实现.如果检查源,则可以看到以下内容:

I'm sorry to say this looks like an incomplete implementation of BarEntry. If you inspect the source, you can see the following:

  1. BarEntry扩展Entry
  2. Entry实现Parcelable
  3. BarEntry没有自己的Parcelable<Creator>
  1. BarEntry extends Entry
  2. Entry implements Parcelable
  3. BarEntry doesn't have its own Parcelable<Creator>

因此,当您序列化成一个包裹然后进行反序列化时,将得到一个List<Entry>而不是List<BarEntry>:-)

So when you serialise into a parcel, and then deserialise, you'll get a List<Entry> instead of List<BarEntry> :-)

目前,除了解决此问题外,您无能为力.

For now there is nothing you can do apart from work around this.

您可以借此机会进行重构,以实现更好的层分离. BarEntry是视图层或视图模型层的成员.除了应该绕过BarEntry,您可能应该有一个清晰的模型层,它仅仅是一个数据对象.您可以轻松地在Intent中传递该模型层的列表或数组,并且使用者可以根据需要转换为BarEntry.

You can take the opportunity to refactor to achieve a better separation of layers. BarEntry is a member of the view layer or the view model layer. Instead of passing around BarEntry, you should probably have a clear model layer which is a mere data object. You can pass lists or arrays of this model layer around in an Intent easily and the consumers can convert to BarEntry as necessary.

或者,创建数据源的抽象.类似于TemperatureRepository类.然后,使用此数据的活动"和片段"可以从存储库而不是从Intent中获取数据.

Alternatively, create an abstraction of a data source. Something like a TemperatureRepository class. Then Activitys and Fragments that consume this data can get the data from the repository rather than from an Intent.

这篇关于无法将MPAndroidChart条目强制转换为BarEntry的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆