如何保存的ArrayList<&经纬度GT;使用共享preferences经纬度、LT、ArrayList、GT

2023-09-06 08:30:25 作者:︶凉夏不忧伤

我一直在寻找这一点 - 但发现只有如何保存的ArrayList比如这个code:

 编辑器prefsEditor = M prefs.edit(); GSON GSON =新GSON(); JSON字符串= gson.toJson(myObject的); prefsEditor.putString(MyObject来,JSON); prefsEditor.commit(); 

我要存储这个对象(点),这是在共享preferences的LatLng,在那之后我需要从这个拯救得到它。以下是我想要保存:

 公共无效onMapClick(经纬度点){    // TODO自动生成方法存根    mMap.addMarker(。新的MarkerOptions()位置(点));    ** checkPoints.add(点); **} 

解决方案

而不是使用preferences,你可以使用SQLite数据库。但是,如果你坚持使用共享preferences那么我不认为你有太多的选择,你只能存储布尔型,浮点型,整型,长字符串或StringSet。唯一可能的办法,我看到的是,您连接所有的值用自己喜欢分离

  1123.4456:1234.223 | 1123.4456:1234.1233 | 1123.4456:1234.223 | 1123.4456:1234.223 
excel函数用法

,然后检索时,它只是解析。

I was looking for this - but found only how to save an arrayList for example this code:

 Editor prefsEditor = mPrefs.edit();
 Gson gson = new Gson();
 String json = gson.toJson(MyObject);
 prefsEditor.putString("MyObject", json);
 prefsEditor.commit();

I want to store this object(point) which is LatLng in SharedPreferences, after that i will need to get it from this save. Here is what i want to save:

public void onMapClick(LatLng point) {
    // TODO Auto-generated method stub

    mMap.addMarker(new MarkerOptions().position(point));
    **checkPoints.add(point);**
}

解决方案

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 like

1123.4456:1234.223|1123.4456:1234.1233|1123.4456:1234.223|1123.4456:1234.223

and then just parse when retrieving it.