Android 如何将“marker.getId()”保存到SQlite数据库?
我试图将标记附带的额外数据保存到SQlite数据库。因此,我面临的问题是,应用程序因以下错误而崩溃:Android 如何将“marker.getId()”保存到SQlite数据库?,android,sqlite,google-maps,Android,Sqlite,Google Maps,我试图将标记附带的额外数据保存到SQlite数据库。因此,我面临的问题是,应用程序因以下错误而崩溃: 12-22 09:12:04.096: E/SQLiteLog(17668): (1) no such column: img 12-22 09:12:04.106: W/dalvikvm(17668): threadid=23: thread exiting with uncaught exception (group=0x41816da0) 12-22 09:12:04.106: D/Abs
12-22 09:12:04.096: E/SQLiteLog(17668): (1) no such column: img
12-22 09:12:04.106: W/dalvikvm(17668): threadid=23: thread exiting with uncaught exception (group=0x41816da0)
12-22 09:12:04.106: D/AbsListView(17668): onVisibilityChanged() is called, visibility : 4
12-22 09:12:04.106: D/AbsListView(17668): unregisterIRListener() is called
12-22 09:12:04.106: E/AndroidRuntime(17668): FATAL EXCEPTION: ModernAsyncTask #1
12-22 09:12:04.106: E/AndroidRuntime(17668): Process: com.example.mainapp, PID: 17668
12-22 09:12:04.106: E/AndroidRuntime(17668): java.lang.RuntimeException: An error occured while executing doInBackground()
12-22 09:12:04.106: E/AndroidRuntime(17668): at android.support.v4.content.ModernAsyncTask$3.done(ModernAsyncTask.java:137)
12-22 09:12:04.106: E/AndroidRuntime(17668): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
12-22 09:12:04.106: E/AndroidRuntime(17668): at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
12-22 09:12:04.106: E/AndroidRuntime(17668): at java.util.concurrent.FutureTask.run(FutureTask.java:242)
12-22 09:12:04.106: E/AndroidRuntime(17668): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
12-22 09:12:04.106: E/AndroidRuntime(17668): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
12-22 09:12:04.106: E/AndroidRuntime(17668): at java.lang.Thread.run(Thread.java:841)
12-22 09:12:04.106: E/AndroidRuntime(17668): Caused by: android.database.sqlite.SQLiteException: no such column: img (code 1): , while compiling: SELECT _id, lat, lng, zom, img FROM locations
现在,我在LocationsDB.java类中放置了一列:
然后
@Override
public void onCreate(SQLiteDatabase db) {
String sql = "create table " + DATABASE_TABLE + " ( " +
FIELD_ROW_ID + " integer primary key autoincrement , " +
FIELD_LNG + " double , " +
FIELD_LAT + " double , " +
FIELD_ZOOM + " text " +
FIELD_IMAGE + "img" + //this line here added
" ) ";
db.execSQL(sql);
}
以及这里:
public Cursor getAllLocations(){
return mDB.query(DATABASE_TABLE, new String[] { FIELD_ROW_ID, FIELD_LAT , FIELD_LNG, FIELD_ZOOM, FIELD_IMAGE } , null, null, null, null, null, null); //FIELD_IMAGE added and null at the end
然后在我的MainActivity课程中,我添加了:
ContentValues contentValues = new ContentValues();
contentValues.put(LocationsDB.FIELD_LAT, point.latitude );
contentValues.put(LocationsDB.FIELD_LNG, point.longitude);
contentValues.put(LocationsDB.FIELD_IMAGE, markerId + bitmap); //this line here added
contentValues.put(LocationsDB.FIELD_ZOOM, googleMap.getCameraPosition().zoom);
只是为了补充一点,我使用HashMap来显示特定标记的图像
private Map<String, Bitmap> myMarkersHash;
private String markerId;
....
myMarkersHash = new HashMap<String, Bitmap>();
....
Marker marker = googleMap.addMarker(new MarkerOptions()
.position(thePoint));
markerId = marker.getId();
....
Bitmap bitmap = myMarkersHash.get(marker.getId());
markerIcon.setImageBitmap(bitmap);
我做错了什么?昨天我发布了一个类似的问题,但是我决定用我添加的代码来问一个新问题,而不是仅仅用没有代码的代码来让别人试着弄清楚我在做什么
谢谢您忘记了,字段后加逗号+文本+
更正:
如果您最近添加了此专栏,则必须卸载应用程序并重新安装新构建的apk。首先,您必须更正创建数据库的方式。img字段不存在,并且您没有创建有效的数据库字段img不是类型。修复数据库创建:
@Override
public void onCreate(SQLiteDatabase db) {
String sql = "create table " + DATABASE_TABLE + " ( " +
FIELD_ROW_ID + " integer primary key autoincrement , " +
FIELD_LNG + " double , " +
FIELD_LAT + " double , " +
FIELD_ZOOM + " text, " + //Here forget a comma
FIELD_IMAGE + " text" + //Here forget a space and you had put bad sqlite type, now text
" ) ";
db.execSQL(sql);
}
最后,您必须卸载并重新安装应用程序。很酷,谢谢,现在真的很尴尬:-我是否正确保存了该标记的图像?您必须使用两个字段:标记id文本或数字和imgtext引用路径或blob(如果数据库上为二进制),好的,但我不确定如何做?这是我第一次使用SQlite。你能告诉我怎么做吗?添加字段标记ID和字段图像。在FIELD_MARKER_ID save ID MARKER和FIELD_IMAGE中保存图像。我已经这样做了,但是我尝试保存的图像是位图,因此我在写入contentValues时得到并出错错误是方法putString,contentValues类型中的字符串不适用于参数String,bitmap
public void onLoadFinished(Loader<Cursor> arg0, Cursor arg1) {
int locationCount = 0;
double lat=0;
double lng=0;
float zoom=0;
float img=0; //This Line here added
locationCount = arg1.getCount();
arg1.moveToFirst();
for(int i=0;i<locationCount;i++){
lat = arg1.getDouble(arg1.getColumnIndex(LocationsDB.FIELD_LAT));
lng = arg1.getDouble(arg1.getColumnIndex(LocationsDB.FIELD_LNG));
zoom = arg1.getFloat(arg1.getColumnIndex(LocationsDB.FIELD_ZOOM));
img = arg1.getFloat(arg1.getColumnIndex(LocationsDB.FIELD_IMAGE)); //This line here added
thePoint = new LatLng(lat, lng);
drawMarker(thePoint);
arg1.moveToNext();
}
if(locationCount>0){
googleMap.moveCamera(CameraUpdateFactory.newLatLng(new LatLng(lat,lng)));
googleMap.animateCamera(CameraUpdateFactory.zoomTo(zoom));
bitmap = myMarkersHash.get(img); //This line here added
}
}
String sql = "create table " + DATABASE_TABLE + " ( " +
FIELD_ROW_ID + " integer primary key autoincrement , " +
FIELD_LNG + " double , " +
FIELD_LAT + " double , " +
FIELD_ZOOM + " text, " +
FIELD_IMAGE + "img" + //this line here added
" ) ";
@Override
public void onCreate(SQLiteDatabase db) {
String sql = "create table " + DATABASE_TABLE + " ( " +
FIELD_ROW_ID + " integer primary key autoincrement , " +
FIELD_LNG + " double , " +
FIELD_LAT + " double , " +
FIELD_ZOOM + " text, " + //Here forget a comma
FIELD_IMAGE + " text" + //Here forget a space and you had put bad sqlite type, now text
" ) ";
db.execSQL(sql);
}