简体   繁体   中英

android - How to retrieve MPAndroidChart ArrayList from file

I'm using MPAndroidChart in my app and I'd like to save an ArrayList in a file, for later use. Here is where I save it to a file in the internal storage:

ArrayList<Entry> entries = getIntent().getParcelableArrayListExtra(getString(R.string.entries_key));

// Save chart data
FileOutputStream out;
try {
  out = openFileOutput("listFile", MODE_PRIVATE);

  ObjectOutputStream outputStream = new ObjectOutputStream(out);
  outputStream.writeObject(entries);
  outputStream.close();

  } catch (Exception e) {
     e.printStackTrace();
  } finally {
  try {

    if (out != null) {
      out.close();
    }

  } catch (IOException e) {
    e.printStackTrace();
  }
}

And here is where I try to retrieve it, but the returned ArrayList doesn't contain any data and the default text ("No chart data available") in my LineChart :

File listFile = new File(getFilesDir(), "listFile");

if(listFile.exists()) {

    FileInputStream inputStream = null;
    try {

        inputStream = openFileInput("listFile");
        ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);

        // This line is evil
        ArrayList <Entry> entries = (ArrayList<Entry>) objectInputStream.readObject();

        // Set chart properties
        chart = ChartUtils.setChartProperties(chart);

        LineDataSet lineDataSet = ChartUtils.createSet(ChartViewer.this, entries);

        LineData data = new LineData(lineDataSet);
        chart.setData(data);

        // Let the chart knows data has changed
        chart.notifyDataSetChanged();
        chart.invalidate();

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
}

I've doubled check it and I'm sure I'm reading in the correct file so the problem must be this line

ArrayList <Entry> entries = (ArrayList<Entry>) objectInputStream.readObject();

I found this way of reading ArrayList from file here How to write an ArrayList to file and retrieve it?

What can I do to solve this problem? Thanks

Documentation for ObjectOutputStream says:

Only objects that support the java.io.Serializable interface can be written to streams

The ArrayList implements the Serializable interface, but it refers to the Entry class which doesn't. So, Entry objects are not serialized, (ie, not saved or restored).

I solved it in an hacky way.. I created a custom MyEntry class that implements Serializable

public class MyEntry implements Serializable {

   private float x;
   private float y;

   public MyEntry(float x, float y) {
       this.x = x;
       this.y = y;
   }

   public MyEntry(){

   }

   public float getX() {
       return x;
   }

   public float getY() {
       return y;
   }

   public void setX(float x) {
       this.x = x;
   }

   public void setY(float y) {
       this.y = y;
   }
} 

Than, when I need to save the data I get them from the ArrayList<Entry> and store them in an ArrayList<MyEntry>

// Save chart data
FileOutputStream out = null;

out = openFileOutput(recordName, MODE_PRIVATE);
ObjectOutputStream outputStream = new ObjectOutputStream(out);
ArrayList<MyEntry> myEntries = new ArrayList<>();

for (int k = 0; k < entries.size(); k++) {
  Entry currentEntry = entries.get(k);
  MyEntry currentMyEntry = new MyEntry();
  currentMyEntry.setX(currentEntry.getX());
  currentMyEntry.setY(currentEntry.getY());
  myEntries.add(currentMyEntry);
}

outputStream.writeObject(myEntries);
outputStream.close();

And when I need to read the values back I take them from MyEntry object and put in an Entry object

FileInputStream inputStream;

inputStream = openFileInput(fileListName);
ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);

ArrayList <MyEntry> myEntries = (ArrayList<MyEntry>) objectInputStream.readObject();
ArrayList<Entry> entries = new ArrayList<>();
for (int i = 0; i < myEntries.size(); i++) {
  MyEntry currentMyEntry = myEntries.get(i);
  Entry currentEntry = new Entry();
  currentEntry.setX(currentMyEntry.getX());
  currentEntry.setY(currentMyEntry.getY());
  entries.add(currentEntry);
}

// Set chart properties
chart = ChartUtils.setChartProperties(chart);

LineDataSet lineDataSet = ChartUtils.createSet(ChartViewer.this, entries);

LineData data = new LineData(lineDataSet);
chart.setData(data);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM