我希望将Google地图v2中的标记保存到SQLite数据库,并在每次返回并再次运行时加载它们
这是我的代码:
public class MapReminderActivity extends FragmentActivity {
Context context = this;
private GoogleMap map;
MarkerDataSource Mdata;
MyMarkerObj Mobj;
final ContentValues contentValues = new ContentValues();
boolean dbCheckedOpen = false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
Mdata = new MarkerDataSource(context);
Mobj = new MyMarkerObj();
try {
Mdata.open();
dbCheckedOpen = true;
}
catch (Exception e) {
}
Cursor cursor = Mdata.db.query(LocationsDB.DATABASE_TABLE, Mdata.cols, null, null, null, null, null);
if (cursor != null) {
List<MyMarkerObj> mmo = Mdata.getMarkers();
for (int i = 0; i < mmo.size(); i++)
{
String latidutes = mmo.get(i).getLat().toString();
String langidutes = mmo.get(i).getLng().toString();
LatLng latlngs = new LatLng(Double.valueOf(latidutes), Double.valueOf(langidutes));
map.addMarker(new MarkerOptions()
.title(mmo.get(i).getTitle().toString())
.position(latlngs));
}//for
}//if
testDialog();
}//onCreate
public void testDialog()
{
map.setOnMapLongClickListener(new OnMapLongClickListener() {
@Override
public void onMapLongClick(final LatLng point) {
if (dbCheckedOpen == false)
{
try {
Mdata.open();
}
catch (Exception e) {
}
}
Mobj.setLat(String.valueOf(point.latitude));
Mobj.setLng(String.valueOf(point.longitude));
final LatLng latlng = new LatLng(Double.valueOf(Mobj.getLat()), Double.valueOf(Mobj.getLng()));
final Dialog reminderdialog = new Dialog(MapReminderActivity.this);
reminderdialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
reminderdialog.setContentView(R.layout.dialog_reminder);
Button btnNextt = (Button) reminderdialog.findViewById(R.id.button1);
final EditText edtTitle = (EditText) reminderdialog.findViewById(R.id.edtTitle);
// Setting latitude in ContentValues
contentValues.put(LocationsDB.FIELD_LAT, Mobj.getLat());
// Setting longitude in ContentValues
contentValues.put(LocationsDB.FIELD_LNG, Mobj.getLng());
reminderdialog.show();
btnNextt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Mdata.addMarker();
Mdata.close();
dbCheckedOpen = false;
reminderdialog.dismiss();
}
}); //setOnclicklistener
}//onMapLongClick
}); //setOnMapLongClickListener
}//reminderDialog
}
这是LocationsDB类:
public class LocationsDB extends SQLiteOpenHelper {
/** Database name */
private static final String DBNAME = "markerlocations.db";
/** Version number of the database */
private static final int VERSION = 1;
/** Field 1 of the table locations, which is the primary key */
public static final String FIELD_ROW_ID = "_id";
/** Field 2 of the table locations, stores the latitude */
public static final String FIELD_LAT = "lat";
/** Field 3 of the table locations, stores the longitude */
public static final String FIELD_LNG = "lng";
/** A constant, stores the the table name */
public static final String DATABASE_TABLE = "locations";
public static final String DB_CREATE = "create table " + DATABASE_TABLE + " ( " +
FIELD_ROW_ID + " integer primary key autoincrement , " +
FIELD_LNG + " text , " +
FIELD_LAT + " text , " +
" ) ";
/** Constructor */
public LocationsDB(Context context) {
super(context, DBNAME, null, VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(DB_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (db == null) {
db.execSQL(DATABASE_TABLE);
onCreate(db);
}
}
}
这是MarkerDataSource类:
public class MarkerDataSource {
LocationsDB mDB;
SQLiteDatabase db;
final String[] cols = { LocationsDB.FIELD_LAT, LocationsDB.FIELD_LNG };
MapReminderActivity main;
public MarkerDataSource(Context C) {
mDB = new LocationsDB(C);
}
public void open() throws SQLException
{
db = mDB.getWritableDatabase();
}
public void close()
{
db.close();
}
public void addMarker()
{
main = new MapReminderActivity();
db.insert(LocationsDB.DATABASE_TABLE, null, main.contentValues);
}
public List<MyMarkerObj> getMarkers() {
List<MyMarkerObj> markers = new ArrayList<MyMarkerObj>();
Cursor cursor = db.query(LocationsDB.DATABASE_TABLE, cols, null, null, null, null, null);
cursor.moveToFirst();
while ( !cursor.isAfterLast())
{
MyMarkerObj mmo = cursorToMarker(cursor);
markers.add(mmo);
cursor.moveToNext();
}
cursor.close();
return markers;
}
private MyMarkerObj cursorToMarker(Cursor cursor) {
MyMarkerObj mmo = new MyMarkerObj();
mmo.setLat(cursor.getString(0));
mmo.setLng(cursor.getString(1));
return mmo;
}
}
这是MyMarkerObj类:
public class MyMarkerObj {
private Long id;
private String lat;
private String lng;
public MyMarkerObj() {
// TODO Auto-generated constructor stub
}
public MyMarkerObj(Long id, String lat, String lng)
{
this.setId(id);
this.setLat(lat);
this.setLng(lng);
}
public MyMarkerObj(String lat, String lng)
{
this.setLat(lat);
this.setLng(lng);
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getLat() {
return lat;
}
public void setLat(String lat) {
this.lat = lat;
}
public String getLng() {
return lng;
}
public void setLng(String lng) {
this.lng = lng;
}
}
但在我的Logcat中显示此错误:
android.database.sqlite.SQLiteException: near "null": syntax error (code 1): , while compiling: INSERT INTO locations(null) VALUES (NULL)
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1467)
at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)
at com.AminApp.Map_Reminder.MarkerDataSource.addMarker(MarkerDataSource.java:51)
at com.AminApp.Map_Reminder.MapReminderActivity$5$1.onClick(MapReminderActivity.java:282)
at android.view.View.performClick(View.java:4204)
at android.view.View$PerformClick.run(View.java:17355)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
我很困惑!
我不知道我在哪里犯了错误。如果我走错了,请帮忙。