java - Saving TimePicker data in a sqlite database -
ok - i'm lost @ point...
i have xml layout user can input several settings. goal able save these settings , retrieve them @ later date - i've done until point. have sqlite database stores several values including currenthour timepicker xml layout outputs settings saved in database.
currently - timepicker saves currenthour database , string i'm using display in view_country xml file.
what i'd display entire time - not currenthour (ex. currenthour : currentminute in order display full time 4:00am) string can use display time in xml layout.
p.s.
i need straightforward answer on how can done (just assume know nothing) i've been trying accomplish several days , still need assistance getting currentminute displayed next currenthour.
i've made several attempts @ @ time feel best revert last known working state (i've been getting force close issues when attempting different methods of concatenating 2 fields) , ask help. (i know can't hard store , display time value timepicker - of more skilled stackoverflow ninjas)
add_country.xml (where timepicker buttons are)
<?xml version="1.0" encoding="utf-8"?> <scrollview xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1"> <linearlayout android:id="@+id/linearlayout" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" android:padding="5dp"> <edittext android:id="@+id/nameedit" android:layout_width="fill_parent" android:layout_height="wrap_content" android:imeoptions="actionnext" android:hint="@string/name_hint" android:inputtype="textpersonname|textcapwords"/> <edittext android:id="@+id/capedit" android:layout_width="fill_parent" android:layout_height="wrap_content" android:imeoptions="actionnext" android:hint="@string/cap_hint" android:inputtype="textpersonname|textcapwords"/> <textview android:id="@+id/textview3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="data limit" android:textcolor="#ffffff" android:textappearance="?android:textappearancemedium" /> <seekbar android:id="@+id/seekbar1" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <linearlayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1.0" android:gravity="left" android:textcolor="#ffffff" android:text="10mb" /> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1.0" android:gravity="right" android:textcolor="#ffffff" android:text="unlimited data" /> </linearlayout> <textview android:id="@+id/textview3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="bandwidth limit" android:textcolor="#ffffff" android:textappearance="?android:textappearancemedium" /> <seekbar android:id="@+id/seekbar1" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <linearlayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1.0" android:gravity="left" android:textcolor="#ffffff" android:text="10kbs" /> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1.0" android:textcolor="#ffffff" android:gravity="right" android:text="unlimited bandwidth" /> </linearlayout> <textview android:id="@+id/textview02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textappearance="?android:textappearancesmall" /> <textview android:id="@+id/textview02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="wifi time limit" android:textcolor="#ffffff" android:textappearance="?android:textappearancemedium" /> <timepicker android:id="@+id/timeedit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:gravity="center" android:layout_weight="1.0" /> <edittext android:id="@+id/codeedit" android:inputtype="texturi" android:layout_width="fill_parent" android:layout_height="wrap_content" android:ems="10" android:lines="1" android:hint="@string/code_hint" android:imeoptions="actionnext" /> <button android:id="@+id/savebtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margintop="15dp" android:layout_gravity="center_horizontal" android:text="@string/save_btn"/> </linearlayout> </scrollview>
addeditcountry.java (java file layout containing timepicker buttons)
import android.app.activity; import android.app.alertdialog; import android.os.asynctask; import android.os.bundle; import android.view.viewgroup; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.edittext; import android.widget.framelayout; import android.widget.timepicker; public class addeditcountry extends activity { private long rowid; private edittext nameet; private edittext capet; private edittext codeet; private timepicker timeet; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.add_country); nameet = (edittext) findviewbyid(r.id.nameedit); capet = (edittext) findviewbyid(r.id.capedit); codeet = (edittext) findviewbyid(r.id.codeedit); timeet = (timepicker) findviewbyid(r.id.timeedit); bundle extras = getintent().getextras(); if (extras != null) { rowid = extras.getlong("row_id"); nameet.settext(extras.getstring("name")); capet.settext(extras.getstring("cap")); codeet.settext(extras.getstring("code")); string time = extras.getint("time"); string[] parts = time.split(":"); timeet.setcurrenthour(parts[0]); timeet.setcurrentminute(parts[1]); } button savebutton =(button) findviewbyid(r.id.savebtn); savebutton.setonclicklistener(new onclicklistener() { public void onclick(view v) { if (nameet.gettext().length() != 0) { asynctask<object, object, object> savecontacttask = new asynctask<object, object, object>() { @override protected object doinbackground(object... params) { savecontact(); return null; } @override protected void onpostexecute(object result) { finish(); } }; savecontacttask.execute((object[]) null); } else { alertdialog.builder alert = new alertdialog.builder(addeditcountry.this); alert.settitle(r.string.errortitle); alert.setmessage(r.string.errormessage); alert.setpositivebutton(r.string.errorbutton, null); alert.show(); } } }); } private void savecontact() { databaseconnector dbconnector = new databaseconnector(this); if (getintent().getextras() == null) { dbconnector.insertcontact(nameet.gettext().tostring(), capet.gettext().tostring(), timeet.getcurrenthour().tostring() + ":" + timeet.getcurrentminute().tostring(), codeet.gettext().tostring()); } else { dbconnector.updatecontact(rowid, capet.gettext().tostring(), timeet.getcurrenthour().tostring() + ":" + timeet.getcurrentminute().tostring(), codeet.gettext().tostring()); } } }
datbaseconnector.java
import android.content.contentvalues; import android.content.context; import android.database.cursor; import android.database.sqlexception; import android.database.sqlite.sqlitedatabase; public class databaseconnector { private static final string db_name = "worldcountries"; private sqlitedatabase database; private databaseopenhelper dbopenhelper; public databaseconnector(context context) { dbopenhelper = new databaseopenhelper(context, db_name, null, 1); } public void open() throws sqlexception { //open database in reading/writing mode database = dbopenhelper.getwritabledatabase(); } public void close() { if (database != null) database.close(); } public void insertcontact(string name, string cap, string code, string time) { contentvalues newcon = new contentvalues(); newcon.put("name", name); newcon.put("cap", cap); newcon.put("time", time); newcon.put("code", code); open(); database.insert("country", null, newcon); close(); } public void updatecontact(long id, string name, string cap,string code, string time) { contentvalues editcon = new contentvalues(); editcon.put("name", name); editcon.put("cap", cap); editcon.put("time", time); editcon.put("code", code); open(); database.update("country", editcon, "_id=" + id, null); close(); } public cursor getallcontacts() { return database.query("country", new string[] {"_id", "name"}, null, null, null, null, "name"); } public cursor getonecontact(long id) { return database.query("country", null, "_id=" + id, null, null, null, null); } public void deletecontact(long id) { open(); database.delete("country", "_id=" + id, null); close(); } }
view_country.xml (where currenthour displayed @ moment - needs display currenthour , currentminute 1 string can display time)
<?xml version="1.0" encoding="utf-8"?> <tablelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:stretchcolumns="1" android:layout_margin="5dp"> <tablerow> <textview style="@style/stylelabel" android:text="@string/name_lbl"/> <textview android:id="@+id/nametext" style="@style/styletext"/> </tablerow> <tablerow> <textview style="@style/stylelabel" android:text="@string/cap_lbl"/> <textview android:id="@+id/captext" style="@style/styletext"/> </tablerow> <tablerow> <textview style="@style/stylelabel" android:text="time limit"/> <textview android:id="@+id/codetext" style="@style/styletext"/> </tablerow> <tablerow> <textview style="@style/stylelabel" android:text="linked users"/> <textview android:id="@+id/codetext" style="@style/styletext"/> </tablerow> <tablerow> <textview style="@style/stylelabel" android:text="@string/code_lbl"/> <textview android:id="@+id/timeedit" style="@style/styletext"/> </tablerow> </tablelayout>
logcat:
03-25 12:50:45.175: d/activity(10602): activity.onpause(), edittexttapsensorlist size: 0 03-25 12:50:45.265: i/adreno200-eglsub(10602): <configwindowmatch:2165>: format rgba_8888. 03-25 12:50:45.276: d/memalloc(10602): ion: mapped buffer base:0x5ca42000 size:614400 offset:0 fd:57 03-25 12:50:45.276: e/(10602): can't open file reading 03-25 12:50:45.276: e/(10602): can't open file reading 03-25 12:50:45.316: d/memalloc(10602): ion: mapped buffer base:0x5d12f000 size:614400 offset:0 fd:61 03-25 12:50:45.326: w/iinputconnectionwrapper(10602): showstatusicon on inactive inputconnection 03-25 12:50:45.326: w/iinputconnectionwrapper(10602): getextractedtext on inactive inputconnection 03-25 12:50:47.248: d/memalloc(10602): ion: unmapping buffer base:0x5ca42000 size:614400 03-25 12:50:47.248: d/memalloc(10602): ion: unmapping buffer base:0x5d12f000 size:614400 03-25 12:50:47.288: d/activity(10602): activity.onpause(), edittexttapsensorlist size: 0 03-25 12:50:47.318: i/adreno200-eglsub(10602): <configwindowmatch:2165>: format rgba_8888. 03-25 12:50:47.318: d/memalloc(10602): ion: mapped buffer base:0x5cd67000 size:614400 offset:0 fd:58 03-25 12:50:47.388: d/memalloc(10602): ion: mapped buffer base:0x5d05f000 size:614400 offset:0 fd:63 03-25 12:50:47.388: w/iinputconnectionwrapper(10602): getextractedtext on inactive inputconnection 03-25 12:50:48.549: d/memalloc(10602): ion: mapped buffer base:0x5d12f000 size:614400 offset:0 fd:66
you can add minutes keeping 1 column in database , 1 string. must split time
string, when setting timepicker
string time = extras.getstring("time"); string[] parts = time.split(":"); timeet.setcurrenthour(integer.valueof(parts[0])); timeet.setcurrentminute(integer.valueof(parts[1]));
and concat it, when inserting or updating
dbconnector.insertcontact(nameet.gettext().tostring(), capet.gettext().tostring(), timeet.getcurrenthour().tostring() + ":" + timeet.getcurrentminute().tostring(), codeet.gettext().tostring());
or keep separate hour
, minute
. means extending database schema , databaseconnector
cope additional parameter, avoids splitting string, when need parts.
Comments
Post a Comment