1

Currently I have an app that tracks user location and draws the route by using polylines and map markers, I need to add the arraylist that contains the LatLng coordinates to another array list that stores all routes, i.e the latlng arraylist is one route, so i need to store all routes in one arraylist and store that in shared preferences so i can load all the routes the user has taken to a map. What I have so far stores just one route to the shared preferences and overwrites it every time a new route is added.

Here is what i have so far, these 2 classes save the 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 the 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 i = 0; i < 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"));

    }

}
Cœur
  • 37,241
  • 25
  • 195
  • 267
MichaelStoddart
  • 5,571
  • 4
  • 27
  • 49
  • possible duplicate of [How to save an arrayList with SharedPreferences](http://stackoverflow.com/questions/23511821/how-to-save-an-arraylistlatlng-with-sharedpreferences) – Kunu May 08 '15 at 11:39
  • @Kunu that answer describes how to save a single arraylist, i am looking at saving multiple arrays that can be retrieved – MichaelStoddart May 08 '15 at 11:41
  • You can use String concatenation using some special symbol as per your requirement and extract it when you need it. – Kunu May 08 '15 at 11:44

4 Answers4

1

Why do you want it in shared preferences? Why not save it as a (list of) serialized objects, as described in this answer. I believe latlng is not serializable (as it is final), but you can fix it like this 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 is serializable and holds some (in this case only 2, start and end) LatLng points, in a similar setup you can use it to serialize and save arrays or lists of LatLng,

Community
  • 1
  • 1
Jasper
  • 2,389
  • 4
  • 25
  • 40
  • Im still fairly new to android and shared preferences is a method im familiar with for storing data when the user isnt using the app that can be easily retrieved when the user comes back to the app, will this method allow me to do this? – MichaelStoddart May 08 '15 at 11:39
  • 1
    a serialized class allows to save (the state of a) class in the memory. By implementing the methods from the answer in the link, you can save and retrieve it. If you these methods in the onStart for example, you can load data you previously saved when your activity starts up. Big advantage is that you save custom objects or lists of them fairly easy. Also look here: http://developer.android.com/reference/java/io/Serializable.html – Jasper May 08 '15 at 11:44
  • Thank you for your answer, I will accept this as the answer as soon as I have attempted the solution for my self – MichaelStoddart May 08 '15 at 11:45
1

Okay so I managed to do it using shared preferences, the code is a little messy and I dont know how inefficient it is for the operation of the device but here goes

To store the multiple routes i use these 2 classes

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);
    SharedPreferences numberOfRoutes = context.getSharedPreferences("NUM_ROUTES", context.MODE_PRIVATE);
    Editor editor = storeAllRoutes.edit();
    Editor numEdit = numberOfRoutes.edit();


        i = numberOfRoutes.getInt("numOfRoutes",0);
        editor.putString("saveData"+i,saveData.toString());
        i++;
        numEdit.putInt("numOfRoutes",i);
        editor.commit();
        numEdit.commit();
        Toast.makeText(getApplicationContext(), "Journey Saved",
                Toast.LENGTH_SHORT).show();

}

Then to retrieve the data I used

public JSONArray getData(Context context, int i) throws Exception {
    SharedPreferences storeAllRoutes = context.getSharedPreferences("STORED_ROUTES", context.MODE_PRIVATE);
    SharedPreferences numberOfRoutes = context.getSharedPreferences("NUM_ROUTES", context.MODE_PRIVATE);


    if (numberOfRoutes.contains("numOfRoutes")&&i>0) {


            return new JSONArray(storeAllRoutes.getString("saveData" + i, ""));


    } else {


        return new JSONArray();
    }

}


public void parseJSONArray(JSONArray array) throws Exception {

    for (int i = 0; i < 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"));

    }

}

I call the retrieve methods in the onCreate method using:

SharedPreferences numberOfRoutes = this.getSharedPreferences("NUM_ROUTES", this.MODE_PRIVATE);

    try {
        if (numberOfRoutes.contains("numOfRoutes")&&numberOfRoutes.getInt("numOfRoutes",0)>0) {
            for(int i = 1;i<numberOfRoutes.getInt("numOfRoutes",0)+1;i++ )
            {
                allRoutes = getData(this,i);
                parseJSONArray(allRoutes);
            }

        }
        else{
            Toast.makeText(getApplicationContext(), "No routes exist",
                    Toast.LENGTH_SHORT).show();

        }

    } catch (Exception e) {
        e.printStackTrace();
    }
MichaelStoddart
  • 5,571
  • 4
  • 27
  • 49
0

Instead of using preferences you can use Sqlite database. But if you insist on using SharedPreferences then I don't think you have much choice as you can only store boolean, float, int, long, String or StringSet. The only possible way I see is that you concatenate all of the values with your own separator.

0

You could also make use of JSON format. Simply parse your collection into JSON formatted string, save this string in preferences.

But generaly speaking I would suggest using SQLIte aswell. Would be easier to manage your colelction for you.