Android studio

时间:2017-07-12 21:39:15

标签: java android sqlite google-maps

我一直致力于一个应用程序,用户可以在Android工作室点击GoogleMap上的某个位置,在该位置添加标记。然后它会提示您3个按钮,一个用于关闭,一个用于添加标题和说明,另一个用于添加照片。

如果我只是按下dismiss,我点击的位置的坐标会自动保存在SQLite中,但如果我添加一个注释然后确认并返回到屏幕,则标记不会显示。这没有意义,因为位置肯定会保存到数据库中,但是当我将标题或描述或照片添加到数据库并返回时,它几乎会删除SQLite中的坐标。

添加标记时应保存坐标,使用此代码

mydb = openOrCreateDatabase("locationmarkersqlite", Context.MODE_PRIVATE, null);

    Cursor cursor = mydb.rawQuery("select _id from locations where lat = '" + latiString + "' and lng = '" + lngiString + "'", null);
    if (cursor.moveToFirst()) {
        do {
            lastId = cursor.getString(0);//can be lastId = cursor.getString("_id");

        }
        while (cursor.moveToNext());
    }

数据库由此SQLiteOpenHelper

管理
public class LocationsDB extends SQLiteOpenHelper{

    /** Database name */
    private static String DBNAME = "locationmarkersqlite";

    /** Version number of the database */
    private static 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";
    /** Field 4 of the table locations, stores the zoom level of map*/
    public static final String FIELD_ZOOM = "zom";
    public static final String FIELD_TITLE = "title";
    public static final String FIELD_BODY = "body";
    public static final String FIELD_PHOTO = "photo";



    /** A constant, stores the the table name */
    private static final String DATABASE_TABLE = "locations";

    /** An instance variable for SQLiteDatabase */
    private SQLiteDatabase mDB;  


    /** Constructor */
    public LocationsDB(Context context) {
        super(context, DBNAME, null, VERSION);  
        this.mDB = getWritableDatabase();
    }



    @Override
    public void onCreate(SQLiteDatabase db) {
        String sql =    "create table " + DATABASE_TABLE + " ( " +
                            FIELD_ROW_ID + " integer primary key autoincrement , " +
                            FIELD_LNG + " text , " +
                            FIELD_LAT + " text , " +
                            FIELD_ZOOM + " text ," +
                            FIELD_TITLE + " text ," +
                            FIELD_BODY + " text ," +
                            FIELD_PHOTO + " text " +
                        " ) ";

        db.execSQL(sql);

    }

    /** Inserts a new location to the table locations */
    public long insert(ContentValues contentValues){
        long rowID = mDB.insert(DATABASE_TABLE, null, contentValues);
        return rowID;

    }   

    public int update(ContentValues contentValues,String contactID){
        int cnt = mDB.update(DATABASE_TABLE, contentValues, "_id=" + contactID, null);
        return cnt;
    }


    /** Deletes all locations from the table */
    public int del(){
        int cnt = mDB.delete(DATABASE_TABLE, null , null);      
        return cnt;
    }

    /** Returns all the locations from the table */
    public Cursor getAllLocations(){
        return mDB.query(DATABASE_TABLE, new String[] { FIELD_ROW_ID,  FIELD_LAT , FIELD_LNG, FIELD_ZOOM } , null, null, null, null, null);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

}

关于我应该做什么的任何想法?其他一切都在运行。

0 个答案:

没有答案