android - Storing multiple instances of Arraylist<LatLng> to shared preferences -


currently have app tracks user location , draws route using polylines , map markers, need add arraylist contains latlng coordinates array list stores routes, i.e latlng arraylist 1 route, need store routes in 1 arraylist , store in shared preferences can load routes user has taken map. have far stores 1 route shared preferences , overwrites every time new route added.

here have far, these 2 classes save latlng data

public void addtojsonarray(jsonarray array, latlng item) throws exception {      jsonobject data = new jsonobject();     data.put("latitude", item.latitude);     data.put("longitude", item.longitude);      array.put(data);   }     public void savejourney(context context, jsonarray savedata) {     sharedpreferences storeallroutes = context.getsharedpreferences("stored_routes", context.mode_private);      editor editor = storeallroutes.edit();     editor.putstring("savedata",savedata.tostring());     editor.commit();  } 

and these 2 classes retrieve data

public jsonarray getdata(context context) throws exception{     sharedpreferences storeallroutes = context.getsharedpreferences("stored_routes", context.mode_private);      if(storeallroutes.contains("savedata")) {         return new jsonarray(storeallroutes.getstring("savedata", ""));     } else {         return new jsonarray();     } }  public void parsejsonarray(jsonarray array) throws exception {      for(int = 0; < array.length(); ++i) {         jsonobject item1 = array.getjsonobject(i);           latlng location = new latlng(item1.getdouble("latitude"), item1.getdouble("longitude"));            mmap.addmarker(new markeroptions().position(location).title("start"));      }  } 

why want in shared preferences? why not save (list of) serialized objects, described in this answer. believe latlng not serializable (as final), can fix similar case:

public class taxiroutedata implements serializable{   private double startlat; private double startlong; private double endlat; private double endlong;    public taxiroutedata() { }  public taxiroutedata(latlng startlocation, latlng endlocation) {     this.startlat = startlocation.latitude;     this.startlong = startlocation.longitude;     this.endlat = endlocation.latitude;     this.endlong = endlocation.longitude;    }  public latlng getstartlocation() {     return  new latlng(startlat,startlong); }  public void setstartlocation(latlng startlocation) {     this.startlat = startlocation.latitude;     this.startlong = startlocation.longitude; }  public latlng getendlocation() {     return  new latlng(endlat,endlong); }  public void setendlocation(latlng endlocation) {     this.endlat = endlocation.latitude;     this.endlong = endlocation.longitude;     } 

this class serializable , holds (in case 2, start , end) latlng points, in similar setup can use serialize , save arrays or lists of latlng,


Comments