问题
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);
回答1:
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());
来源:https://stackoverflow.com/questions/50323180/passing-dataset-between-activity-in-mpandroidchart