Reputation: 35
Hye There,
My project to read latitute and longitute in sqlite database. First of all, i cant connect my connection to database sqlite. and second after connection succeed and i dont know how to loop all data in this google map.
Example one data in my database:
_id DateOc TimeOc Location Latitude Longitude
1 5-11-1961 4.30am Ringlet, Cameron Highlands,Pahang 4.415895 101.383082
Thanks In advance who helping me, I appreciate that, sincerely, Hafizul Reza
private GoogleMap mMap; // Might be null if Google Play services APK is not available.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
setUpMapIfNeeded();
}
@Override
protected void onResume() {
super.onResume();
setUpMapIfNeeded();
}
private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
// Check if we were successful in obtaining the map.
if (mMap != null) {
setUpMap();
}
}
}
private void setUpMap() {
int i = 1;
if(i==1){
SQLiteDatabase myDB = null;
String TableName = "Landslide";
//String Data="";
myDB = this.openOrCreateDatabase("Landslide", MODE_PRIVATE, null);
Cursor c = myDB.rawQuery("SELECT * FROM " + TableName, null);
double Column1 = c.getColumnIndex("Latitude");
double Column2 = c.getColumnIndex("Longitude");
//int Column3 = c.getColumnIndex("Location");
mMap.addMarker(new MarkerOptions().position(new LatLng(Column1 , Column2)).title("Zasss"));
}
//Loop through all Results
//do {
//Data =Data +a+"|"+b+"\n";
// }while(c.moveToNext());
//}
//double lat = 4.415895;
//double lng = 101.383082;
else
//mMap.addMarker(new MarkerOptions().position(new LatLng(Column1, Column2)).title("Column3"));
mMap.addMarker(new MarkerOptions().position(new LatLng(3.37376388889, 101.615391667)).title("Kg. Sri Serendah"));
}
}
Upvotes: 0
Views: 1632
Reputation: 2374
You need to create a Database file as shown below:
public class SQLiteDataBaseAdapter extends Activity {
SQLiteHelper helper;
SQLiteDatabase db, db1;
long id, id1, id2;
public SQLiteDataBaseAdapter(Context context) {
helper = new SQLiteHelper(context);
}
// Inserting Data into the DaTaBase
public long insertData(String task_name, String contact_name, String contact_number, String description, String remarks,
String date, String time, String est_comp_date, String est_comp_time, String act_comp_date, String act_comp_time, String notify_date, String notify_time) {
db = helper.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(SQLiteHelper.TASK_NAME, task_name);
contentValues.put(SQLiteHelper.CONTACT_NAME, contact_name);
contentValues.put(SQLiteHelper.CONTACT_NUMBER, contact_number);
//contentValues.put(SQLiteHelper.CONTACT_EMAIL, contact_email);
contentValues.put(SQLiteHelper.DESCRIPTION, description);
contentValues.put(SQLiteHelper.REMARKS, remarks);
contentValues.put(SQLiteHelper.DATE, date);
contentValues.put(SQLiteHelper.TIME, time);
contentValues.put(SQLiteHelper.ESTIMATED_COMPLETION_DATE, est_comp_date);
contentValues.put(SQLiteHelper.ESTIMATED_COMPLETION_TIME, est_comp_time);
contentValues.put(SQLiteHelper.ACTUAL_COMPLETION_DATE, act_comp_date);
contentValues.put(SQLiteHelper.ACTUAL_COMPLETION_TIME, act_comp_time);
contentValues.put(SQLiteHelper.NOTIFY_DATE, notify_date);
contentValues.put(SQLiteHelper.NOTIFY_TIME, notify_time);
id = db.insert(SQLiteHelper.TABLE_NAME, null, contentValues);
Log.d("PaNa", "The value of Id is " + id);
db.close();
return id;
}
public Cursor getAllData() {
db = helper.getWritableDatabase();
String[] columns = {SQLiteHelper.UID,
SQLiteHelper.TASK_NAME,
SQLiteHelper.CONTACT_NAME,
SQLiteHelper.CONTACT_NUMBER,
//SQLiteHelper.CONTACT_EMAIL,
SQLiteHelper.DESCRIPTION,
SQLiteHelper.REMARKS,
SQLiteHelper.DATE,
SQLiteHelper.TIME,
SQLiteHelper.ESTIMATED_COMPLETION_DATE,
SQLiteHelper.ESTIMATED_COMPLETION_TIME,
SQLiteHelper.ACTUAL_COMPLETION_DATE,
SQLiteHelper.ACTUAL_COMPLETION_TIME,
SQLiteHelper.NOTIFY_DATE,
SQLiteHelper.NOTIFY_TIME};
Cursor cursor = db.query(SQLiteHelper.TABLE_NAME, columns, null, null,
null, null, null);
StringBuffer buffer = new StringBuffer();
while (cursor.moveToNext()) {
int cid = cursor.getInt(0);
String taskName = cursor.getString(1);
String est_comp_time = cursor.getString(6);
buffer.append(cid + " " + taskName + " " + est_comp_time + "\n");
}
if (cursor != null) {
cursor.moveToFirst();
}
return cursor;
}
//Obtain a single row
public Task getTask(String id) {
Log.d("Pana", "The value of id is " + String.valueOf(id));
db = helper.getReadableDatabase();
String[] columns = {SQLiteHelper.UID,
SQLiteHelper.TASK_NAME, SQLiteHelper.CONTACT_NAME, SQLiteHelper.CONTACT_NUMBER, SQLiteHelper.DESCRIPTION, SQLiteHelper.REMARKS, SQLiteHelper.DATE, SQLiteHelper.TIME, SQLiteHelper.ESTIMATED_COMPLETION_DATE, SQLiteHelper.ESTIMATED_COMPLETION_TIME,
SQLiteHelper.ACTUAL_COMPLETION_DATE, SQLiteHelper.ACTUAL_COMPLETION_TIME, SQLiteHelper.NOTIFY_DATE, SQLiteHelper.NOTIFY_TIME};
Cursor cursor = db.query(SQLiteHelper.TABLE_NAME, columns, SQLiteHelper.UID + "= ?",
new String[]{String.valueOf(id)}, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
Task task = new Task(cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4), cursor.getString(5),
cursor.getString(6), cursor.getString(7), cursor.getString(8), cursor.getString(9), cursor.getString(10),
cursor.getString(11), cursor.getString(12), cursor.getString(13));
cursor.close();
return task;
}
}
class SQLiteHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "task_management_database";
public static final String TABLE_NAME = "DataTable";
public static final String UID = "_id";
public static final String TASK_NAME = "Task_name";
public static final String CONTACT_NAME = "Contact_Name";
public static final String CONTACT_NUMBER = "Contact_Number";
public static final String CONTACT_EMAIL = "Contact_Email";
public static final String DESCRIPTION = "Description";
public static final String REMARKS = "Remarks";
public static final String DATE = "Date";
public static final String TIME = "Time";
public static final String ESTIMATED_COMPLETION_DATE = "EstCompDate";
public static final String ESTIMATED_COMPLETION_TIME = "EstCompTime";
public static final String ACTUAL_COMPLETION_DATE = "ActCompDate";
public static final String ACTUAL_COMPLETION_TIME = "ActCompTime";
public static final String NOTIFY_DATE = "NotifyDate";
public static final String NOTIFY_TIME = "NotifyTime"
public static final int DATABASE_VERSION = 1;
public static final String SUB_TASK_NUMBER = "sub_task_number";
public static final String CREATE_TABLE = " CREATE TABLE " + TABLE_NAME +
"(" + UID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + TASK_NAME + " VARCHAR(250) UNIQUE," + CONTACT_NAME + " VARCHAR(250)," + CONTACT_NUMBER + " VARCHAR(250),"
+ CONTACT_EMAIL + " VARCHAR(250)," + DESCRIPTION + " VARCHAR(250), " + REMARKS + " VARCHAR(250),"
+ DATE + " VARCHAR(250)," + TIME + " VARCHAR(250)," + ESTIMATED_COMPLETION_DATE + " VARCHAR(250), " + ESTIMATED_COMPLETION_TIME + " VARCHAR(250), "
+ ACTUAL_COMPLETION_DATE + " VARCHAR(250), " + ACTUAL_COMPLETION_TIME + " VARCHAR(250), " + NOTIFY_DATE + " VARCHAR(250), " + NOTIFY_TIME + " VARCHAR(250), " );";
public static final String DROP_TABLE = "DROP TABLE IF EXISTS" + TABLE_NAME;
// private Context context;
public SQLiteHelper(Context context) {
// TODO Auto-generated constructor stub
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// This is the method which is executed first
@Override
public void onCreate(SQLiteDatabase db) {
try {
db.execSQL(CREATE_TABLE);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
try {
db.execSQL(DROP_TABLE);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
onCreate(db);
}
}
I have shown sample example of creating database in android, how to insert data and how to read data. You need to follow this and modify as per your application. The inner class extends SQLiteOpenHelper and the attributes are accessed from Main Class.
Hope this helps you.
Upvotes: 1