问题描述
- 安卓开发:怎样向list里面连续不断地存值,并读取出来?
-
目前需要不断从GPS那获取经纬度值,然后存储到list里面,再然后读取出来,绘制折线。
怎样才能实现不断存入经纬度,然后再读取出来?
如下:
private List mList;
mList=new ArrayList();//实例化list
GeoPoint pt = new GeoPoint((int)(location.getLatitude()*1e6),
(int)(location.getLongitude()*1e6));
我每隔5秒接收一次经纬度数据,然后我对存值那里有些疑问,请各位帮帮忙吧,多谢大家了!
解决方案
是要这个吗?
mList.add(pt);//添加
for(int i=0;i<mList.size();i++){//取值
GeoPoint pt =(GeoPoint)mList.get(i);
}
解决方案二:
序列化,反序列化
public static void serialize(Object obj, String fileName) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
try {
ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
objectOutputStream.writeObject(obj);
FileOutputStream fileOutputStream = new FileOutputStream(fileName);
fileOutputStream.write(byteArrayOutputStream.toByteArray());
fileOutputStream.flush();
objectOutputStream.close();
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static Object deserialize(String fileName) {
Object obj = null;
try {
FileInputStream fileInputStream = new FileInputStream(fileName);
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
try {
obj = objectInputStream.readObject();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
fileInputStream.close();
objectInputStream.close();
} catch (IOException e) {
obj = null;
e.printStackTrace();
}
return obj;
}
时间: 2024-12-05 19:22:07