问题描述
我想获取某个位置的经度和纬度坐标并保存在ArrayList中,然后将此列表提交到SharedPreference文件,因此当地图加载所有以前的位置时,可以使用SharedPreference文件中的数据使用标记来设置
I want to get the longitude and latitude co-ordinates of a location and save in a ArrayList, then commit this list to SharedPreference file so when the Map loads all previous locations could be set with a marker using data from the SharedPreference file.
我只知道SharedPreference将只存储原始数据类型,而不存储数组。
I only know realise that SharedPreference will only store primitive data types and not arrays.
我的问题是有没有人知道通过共享首选技术或通过任何其他方式保留此数据的方法?
My question does is anybody know of a way to persist this data either via shared pref technique or by any alternate means?
任何输入值得赞赏。
推荐答案
1-创建一个类,然后放置要存储的所有内容,例如 arraylist >纬度和经度
,并使该类实现 Serializable
接口。
1- create a class and put everything you want to store for example arraylist
of your latitude
and longitude
and make that class implement Serializable
interface.
class MyBundle implements Serializable {
ArrayList<Double> mLat;
ArrayList<Double> mLan;
MyBundle( ArrayList<Double> latitude, ArrayList<Double> longitude ){
mLat = latitude;
mLan = longitude;
}
}
2-写将其归档:
ObjectOutputStream oos = null;
FileOutputStream fout = null;
try{
FileOutputStream fout = new FileOutputStream("Your file path");
ObjectOutputStream oos = new ObjectOutputStream(fout);
oos.writeObject(mb); // mb is an instance of MyBundle
} catch (Exception ex) {
e.printStackTrace();
}finally {
if(oos != null){
oos.close();
}
}
并取回所有内容:
ObjectInputStream objectinputstream = null;
try {
streamIn = new FileInputStream("Your file address");
objectinputstream = new ObjectInputStream(streamIn);
MyBundle mb = (MyBundle) objectinputstream.readObject();
} catch (Exception e) {
e.printStackTrace();
}finally {
if(objectinputstream != null){
objectinputstream .close();
}
}
这篇关于如何在共享首选项中保存纬度和经度(位置)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!