【问题标题】:Storing multiple instances of Arraylist<LatLng> to shared preferences将 Arraylist<LatLng> 的多个实例存储到共享首选项
【发布时间】:2015-07-19 07:02:57
【问题描述】:

目前我有一个应用程序可以跟踪用户位置并使用折线和地图标记绘制路线,我需要将包含 LatLng 坐标的数组列表添加到另一个存储所有路线的数组列表中, 即 latlng 数组列表是一条路线,因此我需要将所有路线存储在一个数组列表中并将其存储在共享首选项中,以便我可以将用户采取的所有路线加载到地图中。 到目前为止,我只存储了一条到共享首选项的路线,并在每次添加新路线时覆盖它。

这是我目前所拥有的,这 2 个类保存了 latlng 数据

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();

}

这两个类检索数据

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"));

    }

}

【问题讨论】:

  • @Kunu 该答案描述了如何保存单个数组列表,我正在考虑保存多个可以检索的数组
  • 您可以根据需要使用一些特殊符号使用字符串连接,并在需要时提取它。

标签: android google-maps arraylist


【解决方案1】:

为什么要在共享偏好设置中使用它?为什么不将其保存为(列表)序列化对象,如this answer 中所述。我相信 latlng 是不可序列化的(因为它是最终的),但你可以像这个类似的情况一样修复它:

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;     }

这个类是可序列化的,并持有一些(在这种情况下只有 2 个,开始和结束)LatLng 点,在类似的设置中,您可以使用它来序列化和保存 LatLng 的数组或列表,

【讨论】:

  • 我对 android 还很陌生,共享首选项是我熟悉的一种方法,用于在用户不使用应用程序时存储数据,当用户返回应用程序时可以轻松检索,这种方法会吗允许我这样做吗?
  • 一个序列化的类允许在内存中保存(a的状态)类。通过实现链接中答案的方法,您可以保存和检索它。例如,如果您在 onStart 中使用这些方法,您可以加载您之前在活动启动时保存的数据。最大的优势是您可以相当轻松地保存自定义对象或它们的列表。也看这里:developer.android.com/reference/java/io/Serializable.html
  • 感谢您的回答,一旦我尝试了自己的解决方案,我会立即接受这个答案
【解决方案2】:

好的,所以我设法使用共享首选项来做到这一点,代码有点乱,我不知道它对设备的操作有多低效,但这里是

为了存储多条路线,我使用这两个类

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();

}

然后去检索我使用的数据

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"));

    }

}

我在 onCreate 方法中调用检索方法:

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();
    }

【讨论】:

    【解决方案3】:

    您可以使用 Sqlite 数据库,而不是使用首选项。但如果你坚持使用 SharedPreferences,那么我认为你没有太多选择,因为你只能存储 boolean、float、int、long、String 或 StringSet。我看到的唯一可能的方法是使用自己的分隔符连接所有值。

    【讨论】:

      【解决方案4】:

      您也可以使用 JSON 格式。只需将您的集合解析为 JSON 格式的字符串,将此字符串保存在首选项中。

      但一般来说,我建议也使用 SQLIte。为您管理您的收藏会更容易。

      【讨论】:

        猜你喜欢
        • 2020-08-01
        • 2016-04-04
        • 1970-01-01
        • 2013-07-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多