: はここに私のコードです。 database
を使用すると、データを入力して読むことができ、次にアプリケーションを使用するときに再び読むことができます。現在のアプリのライフサイクルでデータのみを永続化したい場合は、カスタムオブジェクトのArrayList
を使用することをおすすめします。 ArrayList<LocationObject>
使い方の簡単な例:
あなたのランニング
Activity
で
public class LocationObject {
private double lng;
private double lat;
private float speed;
private long time;
public LocationObject(long time, double lng, double lat, float speed) {
this.time = time;
this.lat = lat;
this.lng = lng;
this.speed = speed;
}
//put getters & setters here, press `Alt` and `Insert`, choose the getters and setters
}
、メソッド内でグローバル(ないArrayList
を初期化しますが、Activity.class
あなたの内側:今
private ArrayList<LocationObject> locationList;
//in onCreate:
locationList = new ArrayList<LocationObject>();
//whenever you retrieve a location, create a LocationObject and store it into the List:
LocationObject currentLocation = new LocationObject(time, lng, lat, speed);
locationList.add(currentLocation);
、あなたは最後の2を取得したい場合
LocationObject lastLocation = locationList.get(locationList.size() - 2);
LocationObject currentLocation = locationList.get(locationList.size() - 1);
//to get the latitude, don't forget to create the getters&setters in your LocationObject.class!
double lastLat = lastLocation.getLat();
編集:最後の値だけを保存するには、値をagに割り当てます大声で宣言された変数:
private Location oldLocation;
private float[] results;
private ArrayList<float> speedList;
//in oncreate:
speedList = new ArrayList<Float>();
private void updateWithNewLocation(Location location) {
String where = "";
results = new float[100]; //[100] means there can be 100 entries - decrease or increase the number depending on your output
if (location != null) {
double lng = location.getLongitude();
double lat = location.getLatitude();
float speed = location.getSpeed();
long time = location.getTime();
String timeString = getTimeString(time);
speedList.add(speed);
if(oldLocation != null){
Location.distanceBetween(oldLocation.getLatitude(), oldLocation.getLongitude(), lat, lng, results);
}
oldLocation = location;
あなたの問題は何ですか?距離を計算し、データを保存し、配列/アーリーリストにデータを入力しますか? – yennsarah
私はdistanceBetweenを使用すると2つの場所が必要ですが、毎回1つしか取得できません。以前の場所を保存する方法はわかりません。また、私はアンドロイドに慣れ親しんでいます。このコードを書いて、配列を作成して速度を保存するのは難しいです。私はdistanceBetweenが配列リストに格納されていることを知っています。しかし、私はこのメソッドでこれを行う方法を知りません。このメソッドは5秒ごとに実行されるため、データが重複している場合は何もしません。 –