如何存储 ArrayList在 Sqlite 数据库上?
我正在开发一款跟踪用户移动的应用.在每个 onLocationChanged()
上,我将纬度和经度存储在 ArrayList
中,然后在路线上绘制一条折线.现在,当 activity
停止时,我想保存所有用户的活动信息,例如:
I'm developing an app that tracks user movement. On each onLocationChanged()
I store the latitude and longitude in ArrayList<latLng>
and than draw a polyline on the route. Now, when activity
stopped I want to save all users activity information like:
- 距离
- 平均速度
- 持续时间
- 所有 ArrayList 纬度活动点
在Sqlite
数据库上稍后恢复它并用这些点绘制折线.我不知道如何将纬度和经度的ArrayList
存储到数据库中的问题.任何人都可以帮忙吗?
on Sqlite
database to restore it later and draw a polyline with this points.
The problem that I have no idea how to store the ArrayList
of latitude and longitude to the database.
Anyone can help?
我的解决方案编辑,也许会对某人有所帮助!
My solution edit, maybe will help someone!
public class SQLUtils extends SQLiteOpenHelper {
private static final String DB_NAME = "data.db";
private static final int DB_VERSION = 1;
private static final String run_table = "Run_table";
private static final String locationPoints = "Location_points";
public SQLUtils(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
Log.d("DB", "onCreate");
String runTable = "create table " + run_table + "(ID VARCHAR(10), PACE VARCHAR(10), DURATION VARCHAR(10), DISTANCE VARCHAR(10), DATE VARCHAR(10), TIME VARCHAR(10));";
String LocationTable = "create table " + locationPoints + "(ID INTEGER, POSITION INTEGER, Latitude REAL, Longitude REAL);";
db.execSQL(runTable);
db.execSQL(LocationTable);
}
public void insertRunData(String id, String pace,String duration, String distance, String date, String time) {
Log.d("DB", "onInsertRunData");
SQLiteDatabase db = this.getWritableDatabase();
String sql = "INSERT into " + run_table +
" (ID, PACE, DURATION, DISTANCE, DATE, TIME) VALUES ('" + id + "','" + pace + "','" + duration + "','" + distance +
"','" + date + "','" + time + "')";
db.execSQL(sql);
db.close();
}
public void insertLatLng(String id, int position, double latitude, double longitude){
SQLiteDatabase db = this.getWritableDatabase();
String sql = "INSERT into " + locationPoints +
" (ID, POSITION, LATITUDE, LONGITUDE) VALUES ('" + UUID.fromString(id) + "','" + Integer.toString(position) + "','"
+ String.valueOf(latitude) + "','" + String.valueOf(longitude) + "')";
db.execSQL(sql);
db.close();
}
运行片段类:
private void saveSqlRun() {
SQLUtils sqlUtils = new SQLUtils(getActivity());
sqlUtils.insertRunData(currentRun.getId(), currentRun.getAveragePace(), currentRun.getRunDuration(),
currentRun.getRunDistance(), currentRun.getRunDate(),currentRun.getRunTime());
/* Save user Location points */
for (int index = 0; index < currentRun.getLocationPoints().size(); ++index) {
sqlUtils.insertLatLng(currentRun.getId(), index, currentRun.getLocationPoints().get(index).latitude, currentRun.getLocationPoints().get(index).longitude);
}
}
run_rable 就是这样的:
Thats how run_rable looks:
Location_points 就是这样的:
Thats how Location_points looks:
首先阅读Sqlite
那么你应该阅读Content Providers
在您了解如何在 android 中使用 sqlite
之后,设计您的表格.我猜你会有一个带列的表:
After you understood how to use sqlite
with android, design your tables. I'm guessing that you will have a table with columns:
id, userId, lat, lng, timeStamp.
现在,您可以轻松获取每个用户所在的位置和时间,并计算出所有其他用户.
Now you can easily get for each user where he was and when and calculate all the rest.
给你一个关于如何存储 ArrayList
的小提示,然后使用类似的东西:
To give you a small hint of how to store ArrayList
then use something like:
ArrayList<ContentProviderOperation> ops
for (LatLng latLng : list) {
ops.add(ContentProviderOperation.insert(Uri).withValue("userId", userId).withValue("lat", 32.3232)...build()
}
getActivity().getContentResolver().applyBatch("AUTHORITY", ops)