java - How do I store TimePicker Data in my simple record keeping app? -


i've created simple record keeping application using various resources around internet. i'm able store text data - when attempt incorporate timepicker end crashing entire app.

any input on how can implemented (as code examples) appreciated!

data input xml:

<?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/timepicker1"     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> 

data input java:

import android.app.activity; import android.app.alertdialog; import android.os.asynctask; import android.os.bundle; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.edittext;  public class addeditcountry extends activity {   private long rowid;   private edittext nameet;  private edittext capet;  private edittext codeet;     @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);        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"));         }        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(),                   codeet.gettext().tostring());       }       else       {          dbconnector.updatecontact(rowid,             nameet.gettext().tostring(),             capet.gettext().tostring(),              codeet.gettext().tostring());       }    } } 

data output xml:

<?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="@string/code_lbl"/>               <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="time limit"/>               <textview          android:id="@+id/timetext"         style="@style/styletext"/>            </tablerow> </tablelayout> 

data output java:

import android.app.activity; import android.app.alertdialog; import android.content.dialoginterface; import android.content.intent; import android.database.cursor; import android.os.asynctask; import android.os.bundle; import android.view.menu; import android.view.menuinflater; import android.view.menuitem; import android.widget.textview;  public class viewcountry extends activity {     private long rowid;    private textview nametv;    private textview captv;    private textview codetv;      @override    public void oncreate(bundle savedinstancestate)     {       super.oncreate(savedinstancestate);       setcontentview(r.layout.view_country);        setupviews();       bundle extras = getintent().getextras();       rowid = extras.getlong(countrylist.row_id);     }     private void setupviews() {        nametv = (textview) findviewbyid(r.id.nametext);        captv = (textview) findviewbyid(r.id.captext);        codetv = (textview) findviewbyid(r.id.codetext);    }     @override    protected void onresume()    {       super.onresume();       new loadcontacts().execute(rowid);    }      private class loadcontacts extends asynctask<long, object, cursor>     {       databaseconnector dbconnector = new databaseconnector(viewcountry.this);        @override       protected cursor doinbackground(long... params)       {          dbconnector.open();          return dbconnector.getonecontact(params[0]);       }         @override       protected void onpostexecute(cursor result)       {          super.onpostexecute(result);           result.movetofirst();          // column index each data item          int nameindex = result.getcolumnindex("name");          int capindex = result.getcolumnindex("cap");          int codeindex = result.getcolumnindex("code");           nametv.settext(result.getstring(nameindex));          captv.settext(result.getstring(capindex));          codetv.settext(result.getstring(codeindex));           result.close();          dbconnector.close();       }    }       @override    public boolean oncreateoptionsmenu(menu menu)     {       super.oncreateoptionsmenu(menu);       menuinflater inflater = getmenuinflater();       inflater.inflate(r.menu.view_country_menu, menu);       return true;    }     @override    public boolean onoptionsitemselected(menuitem item)     {       switch (item.getitemid())       {          case r.id.edititem:             intent addeditcontact =                new intent(this, addeditcountry.class);              addeditcontact.putextra(countrylist.row_id, rowid);             addeditcontact.putextra("name", nametv.gettext());             addeditcontact.putextra("cap", captv.gettext());             addeditcontact.putextra("code", codetv.gettext());             startactivity(addeditcontact);              return true;           case r.id.deleteitem:             deletecontact();             return true;           default:             return super.onoptionsitemselected(item);       }     }     private void deletecontact()    {        alertdialog.builder alert = new alertdialog.builder(viewcountry.this);        alert.settitle(r.string.confirmtitle);        alert.setmessage(r.string.confirmmessage);         alert.setpositivebutton(r.string.delete_btn,          new dialoginterface.onclicklistener()          {             public void onclick(dialoginterface dialog, int button)             {                final databaseconnector dbconnector =                    new databaseconnector(viewcountry.this);                 asynctask<long, object, object> deletetask =                   new asynctask<long, object, object>()                   {                      @override                      protected object doinbackground(long... params)                      {                         dbconnector.deletecontact(params[0]);                          return null;                      }                        @override                      protected void onpostexecute(object result)                      {                         finish();                       }                   };                 deletetask.execute(new long[] { rowid });                            }          }       );        alert.setnegativebutton(r.string.cancel_btn, null).show();    } } 

database connector 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) {         contentvalues newcon = new contentvalues();         newcon.put("name", name);         newcon.put("cap", cap);         newcon.put("code", code);          open();         database.insert("country", null, newcon);         close();     }       public void updatecontact(long id, string name, string cap,string code)      {         contentvalues editcon = new contentvalues();         editcon.put("name", name);         editcon.put("cap", cap);         editcon.put("code", code);          pen();         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();     } } 

database helper java:

import android.content.context; import android.database.sqlite.sqlitedatabase; import android.database.sqlite.sqlitedatabase.cursorfactory; import android.database.sqlite.sqliteopenhelper;  public class databaseopenhelper extends sqliteopenhelper {      public databaseopenhelper(context context, string name,             cursorfactory factory, int version) {         super(context, name, factory, version);     }      @override     public void oncreate(sqlitedatabase db) {         string createquery = "create table country (_id integer primary key autoincrement,name, cap, code);";                          db.execsql(createquery);             }      @override     public void onupgrade(sqlitedatabase db, int oldversion, int newversion) {      }  } 

in create table statement define type

create table ...... dtfield date, tmpname text..... 

use following saving date text

//sample date format - 2013-03-21 13:12:00 android.text.format.dateformat.format("yyyy-mm-dd hh:mm:ss", dtdate.gettime()) 

Comments