Hello I'm making an app using google maps and I want to be able to save a list so that it can be used opened later by the user. After searching a bit on the site I found that people suggest using gson but I wasn't able to get that to work. I'd be willing to use that but I can't get it to work. Here is my code:
private void loadPoints() throws IOException, ClassNotFoundException {
Reader isReader = new InputStreamReader(new FileInputStream(file));
List<LatLng> list = Collections.synchronizedList(new ArrayList<LatLng>());
Type listOfLatLng = new TypeToken<List<LatLng>>() {
}.getType();
String s = gson.toJson(list, listOfLatLng);
List<LatLng> RetreivedPoints = gson.fromJson(s, listOfLatLng);
isReader.close();
System.out.println(RetreivedPoints.get(0) );
if (RetreivedPoints != null) {
String joined = TextUtils.join(", ", RetreivedPoints);
tvCoords.setText(joined);
}
}
private void savePoints(List<LatLng> l) throws IOException {
gson = new Gson();
Writer osWriter = new OutputStreamWriter(new FileOutputStream((file)));
List<LatLng> list = Collections.synchronizedList(new ArrayList<LatLng>());
list.add(new LatLng(29, 49));
gson.toJson(list, osWriter);
Toast.makeText(MapsActivity.this, "Saved data!", Toast.LENGTH_SHORT).show();
}
I don't get any errors when I savePoints(); but when I loadPoints(); RetreivedPoints is empty.
If you think there is a better approach to this I'd appreciate it if you could comment!