Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
185 views
in Technique[技术] by (71.8m points)

android - Passing dataset between activity in MPAndroidChart

I want to display barchart in full screen after clicking on a button but I got an error when passing BarEntry class between activities.

android.os.ParcelFormatException: Cannot parcel an Entry with non-parcelable data

my code :

ArrayList<BarEntry> barEntries = new ArrayList<>();
        barEntries.clear();
        barEntries.add(new BarEntry(0, new float[]{8f, 9f, 10f}, "Jan"));
        barEntries.add(new BarEntry(1, new float[]{11f, 9f, 5f}, "Feb"));
        barEntries.add(new BarEntry(2, new float[]{8f, 3f, 4f}, "Mar"));
        barEntries.add(new BarEntry(3, new float[]{5f, 14f, 2f}, "Apr"));
        barEntries.add(new BarEntry(4, new float[]{5f, 2f, 3f}, "May"));
        barEntries.add(new BarEntry(5, new float[]{2f, 15f, 6f}, "Jun"));
        barEntries.add(new BarEntry(6, new float[]{15f, 2f, 8f}, "Jul"));
        barEntries.add(new BarEntry(7, new float[]{4f, 19f, 4f}, "Aug"));
        barEntries.add(new BarEntry(8, new float[]{5f, 5f, 5f}, "Sept"));
        barEntries.add(new BarEntry(9, new float[]{6f, 6f, 6f}, "Oct"));
        barEntries.add(new BarEntry(10, new float[]{8f, 9f, 10f}, "Nov"));
        barEntries.add(new BarEntry(11, new float[]{2f, 7f, 7f}, "Dec"));
        barEntries.add(new BarEntry(12, new float[]{6f, 2f, 8f}, "Jan"));

Intent bar = new Intent(ChartRevenueActivity.this, FullscreenActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("barEntries", barEntries);
                bar.putExtra("type", "barChart");
                bar.putExtras(bundle);
                startActivity(bar);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Add this lib to build.gradle,

compile 'com.google.code.gson:gson:2.8.1'

convert your Arraylist to json using Gson and pass the json string to new activity.

Intent bar = new Intent(ChartRevenueActivity.this, FullscreenActivity.class);
    bar.putExtra("type", "barChart");
    bar.putExtra("barentry",new Gson().toJson(barEntries));
    startActivity(bar);

retrieve the Arraylist in new activity like this,

 ArrayList<BarEntry> barEntries = 
            new Gson().fromJson(getIntent().getStringExtra("barentry"), new TypeToken<ArrayList<BarEntry>>(){}.getType());

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...