与另一种类型MPAndroidChart的parcelable对象

时间:2017-03-30 15:20:59

标签: android fragment parcelable mpandroidchart

public class DataForBinding implements Parcelable {

// private static final long serialVersionUID = 1L;
public ArrayList<BarEntry> barEntries;
public ArrayList<CandleEntry> candleEntries;
public ArrayList<Entry> lineEntries;

public DataForBinding() {

}

protected DataForBinding(Parcel in) {
    barEntries=new ArrayList<BarEntry>();
    candleEntries=new ArrayList<CandleEntry>();
    lineEntries=new ArrayList<Entry>();
    this.candleEntries = in.readArrayList(Entry.class.getClassLoader());
    this.barEntries = in.readArrayList(Entry.class.getClassLoader());
    this.lineEntries = in.readArrayList(Entry.class.getClassLoader());
}

public static final Creator<DataForBinding> CREATOR = new Creator<DataForBinding>() {
    @Override
    public DataForBinding createFromParcel(Parcel in) {
        return new DataForBinding(in);
    }

    @Override
    public DataForBinding[] newArray(int size) {
        return new DataForBinding[size];
    }
};

@Override
public int describeContents() {
    return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeList(barEntries);
    dest.writeList(candleEntries);
    dest.writeList(lineEntries);
}                  //set&get

BarEntry扩展条目
CandleEntry扩展条目
 BarEntry扩展条目
条目扩展BaseEntry实现 Parcelable

我用它来发送/接收图表数据。 (第一项活动)

Intent intent = new Intent(getActivity(), MainTabActivity.class);
                data = new DataForBinding();
                data.candleEntries=generateCandleData();
                data.barEntries=generateBarData();
                data.lineEntries=generateLineData();
                intent.putExtra("object",data);
                startActivity(intent);

// --------------------------------------------- - (第二)

  public class MainTabActivity extends AppCompatActivity {
        Toolbar toolbar;
        TabLayout tabLayout;
        ViewPager viewPager;
        ViewPagerAdapter adapter;
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main_tab);
            DataForBinding data=getIntent().getParcelableExtra("object");
            getIntent().putExtra("data_all",data);
               }

我从活动发送到活动的所有数据,以及从第二个活动发送到片段中的片段的所有数据。 但是在片段上他只使用Entry类(lineEntries),然后说 无法将条目转换为com.github.mikephil.charting.data.CandleEntry

 @Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    mChart = (CombinedChart) getView().findViewById(R.id.chart_in_tab);
    DataForBinding data = new DataForBinding();

    ArrayList<BarEntry> barEntries = new ArrayList<>();
    ArrayList<CandleEntry> candleEntries;
    ArrayList<Entry> lineEntries = new ArrayList<>();

    data = getActivity().getIntent().getParcelableExtra("data_all");

    barEntries = data.getBarEntries();
    candleEntries = data.getCandleEntries();
    lineEntries = data.getLineEntries();

    mChart = CombinedChartHelper.setSettings(mChart);

    LineDataSet set2 = new LineDataSet(lineEntries, "line");
    DataHelper.setLineDataSet(set2);
    LineData dataLine = new LineData(set2);

    CandleDataSet set1 = new CandleDataSet(candleEntries, "candle");
    DataHelper.setCandleDataSet(set1);
    CandleData dataCandle = new CandleData(set1);

    CombinedData data = new CombinedData();

    data.setData(dataLine);

1 个答案:

答案 0 :(得分:0)

Entry的子类没有像this question的答案中所指出的那样实现Parcelable。因此,他们不会反序列化为正确的不变量(BarEntryCandleEntry)等,而是反变为协变Entry

您必须找到一些解决方法,通过传递原始数据并在收件人中创建Entry,或者为数据提取公共存储库并让收件人处理该数据