How to save locations from a map to an Sqlite database?

Asked

Viewed 1,457 times

5

I plan to save sites displayed on a map to a Sqlite database so that the user can consult them in the future and add a description. Here’s my class Description...

public class Descricao {
  private long id;
  private String comentario;
  private double mLat;
  private double mLng;

  public long getId() {
    return id;
  }

  public void setId(long id) {
    this.id = id;
  }

  public double getLat() {
    return mLat;
  }

  public void setLat(double lat) {
    this.mLat = lat;
  }

  public double getLng() {
    return mLng;
  }

  public void setLng(double lng) {
    this.mLng = lng;
  }

  public String getComentario(){
    return comentario;
  }

  public void setComentario( String comentario) {
     this.comentario = comentario;
  }

  // Will be used by the ArrayAdapter in the ListView
  @Override
  public String toString() {
     return comentario;
  }
} 

Here is the code part of my Mainactivity.java where I call the latitude and longitude to display the locations on the map:

  mPlaces = (Place[]) savedInstanceState.getParcelableArray("places");              
                for(int i=0;i<mPlaces.length;i++){


                    LatLng point = new LatLng(Double.parseDouble(mPlaces[i].mLat),
                                              Double.parseDouble(mPlaces[i].mLng));


                    Marker m = drawMarker(point,UNDEFINED_COLOR);


                    mHMReference.put(m.getId(), mPlaces[i]);
                }

This is the Mysqliteopenhelper class.java

public class MySQLiteHelper extends SQLiteOpenHelper {

      public static final String TABELA_DESCRICAO = "descricao";
      public static final String COLUMN_ID = "_id";
      public static final String COLUMN_COMENTARIO = "comentario";
      public static final String COLUMN_LAT = "lat";
      public static final String COLUMN_LNG = "lng";
      private static final String DATABASE_NAME = "descricao.db";
      private static final int DATABASE_VERSION = 1;

      // Database creation sql statement
      private static final String DATABASE_CREATE = "create table "
          + TABELA_DESCRICAO + "(" + COLUMN_ID
          + " integer primary key autoincrement, " + COLUMN_COMENTARIO
          + " text not null)" +
          COLUMN_LAT + "double" + COLUMN_LNG + "double"
          + COLUMN_COMENTARIO + "text no null";


      public MySQLiteHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
      }

      @Override
      public void onCreate(SQLiteDatabase basedados) {
        basedados.execSQL(DATABASE_CREATE);
      }

      @Override
      public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.w(MySQLiteHelper.class.getName(),
            "Upgrading database from version " + oldVersion + " to "
                + newVersion + ", which will destroy all old data");
        db.execSQL("DROP TABLE IF EXISTS " + TABELA_DESCRICAO);
        onCreate(db);
      }       
}

This is the Descricaodao.java class that interfaces between the Descricao.java class and Mysqliteopenhelper.java class

public class DescricaoDAO {
public class DescricaoDAO {

   // Database fields
  private SQLiteDatabase database;
  private MySQLiteHelper dbHelper;
  private String[] allColumns = { MySQLiteHelper.COLUMN_ID, MySQLiteHelper.COLUMN_LAT, MySQLiteHelper.COLUMN_LNG,
      MySQLiteHelper.COLUMN_COMENTARIO };

  public DescricaoDAO(Context context) {
    dbHelper = new MySQLiteHelper(context);
  }


public void open() throws SQLException {
    database = dbHelper.getWritableDatabase();
  }

  public void close() {
    dbHelper.close();
  }

  public Descricao createComment(String comentario) {
    ContentValues values = new ContentValues();
    values.put(MySQLiteHelper.COLUMN_COMENTARIO, comentario);
    long insertId = database.insert(MySQLiteHelper.TABELA_DESCRICAO, null,
        values);
    Cursor cursor = database.query(MySQLiteHelper.TABELA_DESCRICAO,
        allColumns, MySQLiteHelper.COLUMN_ID + " = " + insertId, null,
        null, null, null);
    cursor.moveToFirst();
    Descricao newComment = cursorToComment(cursor);
    cursor.close();
    return newComment;
  }

  public void deleteComment(Descricao comentario) {
    long id = comentario.getId();
    System.out.println("Comment deleted with id: " + id);
    database.delete(MySQLiteHelper.TABELA_DESCRICAO, MySQLiteHelper.COLUMN_ID
        + " = " + id, null);
  }

  public List<Descricao> getAllComments() {
    List<Descricao> descricao = new ArrayList<Descricao>();

    Cursor cursor = database.query(MySQLiteHelper.TABELA_DESCRICAO,
        allColumns, null, null, null, null, null);

    cursor.moveToFirst();

    while (!cursor.isAfterLast()) {
      Descricao comentario = cursorToComment(cursor);
      descricao.add(comentario);
      cursor.moveToNext();
    }
    // make sure to close the cursor
    cursor.close();
    return descricao;
  }

  private Descricao cursorToComment(Cursor cursor) {
    Descricao comentario = new Descricao();
    comentario.setId(cursor.getLong(0));
    comentario.setComentario(cursor.getString(1));
    return comentario;
  }
} 
  • What is your doubt?

  • First I would like to know if I am keeping the latitude and longitude variables well, that is, I am keeping the mLat and mLng variables of the Mainacticity.java class in the Descricao.java. It is correct?

1 answer

2

  • Thanks for the nomination... I believe I’m already saving the two variables well. The difficulty now is how to pass the saved Places to a listview and this material you indicated does not explain.... But I appreciate any additional help...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.