google maps - Connection sqlite in android studio -
hye there,
my project read latitute , longitute in sqlite database. first of all, cant connect connection database sqlite. , second after connection succeed , dont know how loop data in google map.
example 1 data in database:
_id dateoc timeoc location latitude longitude
1 5-11-1961 4.30am ringlet, cameron highlands,pahang 4.415895 101.383082
thanks in advance helping me, appreciate that, sincerely, hafizul reza
private googlemap mmap; // might null if google play services apk 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() { // null check confirm have not instantiated map. if (mmap == null) { // try obtain map supportmapfragment. mmap = ((supportmapfragment) getsupportfragmentmanager().findfragmentbyid(r.id.map)) .getmap(); // check if successful in obtaining map. if (mmap != null) { setupmap(); } } } private void setupmap() { int = 1; if(i==1){ sqlitedatabase mydb = null; string tablename = "landslide"; //string data=""; mydb = this.openorcreatedatabase("landslide", mode_private, null); cursor c = mydb.rawquery("select * " + 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 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")); } }
you need create database file 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 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 " + 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 single row public task gettask(string id) { log.d("pana", "the value of id " + 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); } // method 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 insert data , how read data. need follow , modify per application. inner class extends sqliteopenhelper , attributes accessed main class.
hope helps you.
Comments
Post a Comment