0
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 extends Entry
CandleEntry extends Entry
BarEntry extends Entry
Entry extends BaseEntry implements Parcelable

I use this for send/receive data for chart. (First activity)

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

//---------------------------------------------- (Second)

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

All data i sent from activity to activity, and from second activity to fragment in tab. But on fragment he work only with Entry class (lineEntries), and say Entry cannot be cast to 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);
  • If you have issues with Parcealable being sent from one activity/fragment to another, and they are in your own app, you can just use a static reference. Just remember that when you are done, set it to null. It's ok to do it. Some objects are too heavy/hard to use, like Bitmaps for example. – android developer Apr 02 '17 at 09:19

1 Answers1

0

The subclasses of Entry do not implement Parcelable as has been pointed out in the answers to this question. Hence, they will not deserialise into the correct invariants (BarEntry, CandleEntry) etc. but instead into the covariant Entry.

You will have to find some way to work around it, either by passing the raw data and making Entry of it inside the recipient, or extracting a common repository for the data and having the recipients deal with that instead.

Community
  • 1
  • 1
David Rawson
  • 20,912
  • 7
  • 88
  • 124