android - sqLite not storing records for next month -
i developing app. activity opened asking "title", "description ", "venue using google api place picker", "date , time using date , time picker" , clicks on create button causes data saved on database.
the problem when date of current month selected user date picker working fine , storing in db when user selects date of next months, not storing data in db entry.
create.java
package com.example.akshay.eventmanager; import android.app.activity; import android.app.datepickerdialog; import android.app.timepickerdialog; import android.content.intent; import android.os.bundle; import android.text.inputtype; import android.util.log; import android.view.view; import android.widget.button; import android.widget.datepicker; import android.widget.edittext; import android.widget.textview; import android.widget.timepicker; import android.app.timepickerdialog.ontimesetlistener; import android.widget.toast; import com.google.android.gms.common.googleplayservicesnotavailableexception; import com.google.android.gms.common.googleplayservicesrepairableexception; import com.google.android.gms.location.places.place; import com.google.android.gms.location.places.ui.placepicker; import com.google.android.gms.maps.model.latlng; import java.text.simpledateformat; import java.util.calendar; import java.util.locale; import android.app.datepickerdialog.ondatesetlistener; /** * created akshay on 7/12/2015. */ public class create extends activity implements view.onclicklistener { button create; edittext title, desc, time, venue; textview venueinfo; placepicker.intentbuilder intentbuilder; private static final int place_picker_request = 1; public latlng gotlatlng; string placename; string plname; string addr; double gotlat, gotlong; string lat, long; databasehelper mydb; public static final string log_tag = "create"; string gottime = " "; string gotdate = " "; datepickerdialog datepicker; timepickerdialog mytimepicker; simpledateformat dateformatter; string timefordb; string datefordb; int dayofmonth; int monthname; static boolean flag = false; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.create); dateformatter = new simpledateformat("eeee dd", locale.us); showdate(); showtime(); casting(); } public void casting() { create = (button) findviewbyid(r.id.bcreate); venue = (edittext) findviewbyid(r.id.etvenue); title = (edittext) findviewbyid(r.id.ettitle); desc = (edittext) findviewbyid(r.id.etdescription); time = (edittext) findviewbyid(r.id.ettime); time.setinputtype(inputtype.type_null); venue.setonclicklistener(this); time.setonclicklistener(this); create.setonclicklistener(this); venue.setinputtype(inputtype.type_null); } @override public void onclick(view v) { switch (v.getid()) { case r.id.etvenue: try { intentbuilder = new placepicker.intentbuilder(); intent intent = intentbuilder.build(create.this); startactivityforresult(intent, place_picker_request); } catch (googleplayservicesrepairableexception e) { e.printstacktrace(); } catch (googleplayservicesnotavailableexception e) { toast.maketext(create.this, "google play services not available.", toast.length_long) .show(); } break; case r.id.bcreate: string title = title.gettext().tostring(); string desc = desc.gettext().tostring(); string eventtime = time.gettext().tostring(); string place = venue.gettext().tostring(); mydb = new databasehelper(this); int flag = mydb.insertvalues(title, desc, place, lat, long, addr, datefordb, timefordb, dayofmonth, monthname); if (flag == 1) { log.e(log_tag , title); log.e(log_tag , desc); log.e(log_tag , place); log.e(log_tag , lat); log.e(log_tag , long); log.e(log_tag , addr); log.e(log_tag , datefordb); log.e(log_tag , timefordb); log.e(log_tag , string.valueof(dayofmonth)); log.e(log_tag , string.valueof(monthname)); toast.maketext(create.this, string.valueof(dayofmonth), toast.length_long).show(); toast.maketext(create.this, string.valueof(monthname), toast.length_long).show(); toast.maketext(create.this, "sucess", toast.length_long).show(); } else { toast.maketext(create.this, "error adding records", toast.length_long).show(); } break; case r.id.ettime: mytimepicker.show(); datepicker.show(); break; } } protected void onactivityresult(int requestcode, int resultcode, intent data) { if (requestcode == place_picker_request) { if (resultcode == result_ok) { place place = placepicker.getplace(data, this); gotlatlng = place.getlatlng(); plname = (string) place.getname(); addr = (string) place.getaddress(); gotlat = gotlatlng.latitude; gotlong = gotlatlng.longitude; lat = string.valueof(gotlat); long = string.valueof(gotlong); placename = string.format("place: %s", place.getname()); toast.maketext(this, placename, toast.length_long).show(); venue.settext(plname); } } } public void showdate() { calendar cal = calendar.getinstance(); datepicker = new datepickerdialog(this, new ondatesetlistener() { public void ondateset(datepicker view, int year, int monthofyear, int dayofmonth) { calendar newdate = calendar.getinstance(); newdate.set(year, monthofyear, dayofmonth); dayofmonth = dayofmonth; monthname = monthofyear; monthname = monthname+1; monthofyear = monthofyear+1; datefordb = string.valueof(dayofmonth) + "/" + string.valueof(monthofyear) + "/" + string.valueof(year); gotdate = dateformatter.format(newdate.gettime()).tostring(); } }, cal.get(calendar.year), cal.get(calendar.month), cal.get(calendar.day_of_week)); flag = true; } public void showtime() { if (flag == true) { final calendar calender = calendar.getinstance(); mytimepicker = new timepickerdialog(this, new ontimesetlistener() { public void ontimeset(timepicker view, int hourofday, int minute) { calendar newtime = calendar.getinstance(); calender.set(calendar.hour_of_day, hourofday); calender.set(calendar.minute, minute); timefordb = string.valueof(hourofday) + " : " + string.valueof(minute); gottime = gotdate + " @ " + string.valueof(hourofday) + ":" + string.valueof(minute); time.settext(gottime); } }, calender.get((calendar.hour_of_day)), calender.get(calendar.minute), true); } } }
databasehelper.java
package com.example.akshay.eventmanager; import android.content.contentvalues; import android.content.context; import android.database.cursor; import android.database.sqlite.sqlitedatabase; import android.database.sqlite.sqliteopenhelper; import android.util.log; import android.widget.toast; import java.sql.sqlexception; import java.util.calendar; /** * created akshay on 7/12/2015. */ public class databasehelper extends sqliteopenhelper { static final string db_name = "myevents.db"; static final string table_name = "myevents"; public static final string col_1 = "id"; public static final string col_2 = "title"; public static final string col_3 = "description"; public static final string col_4 = "venue"; public static final string col_5 = "latitude"; public static final string col_6 = "longitude"; public static final string col_7 = "address"; public static final string col_8 = "date"; public static final string col_9 = "time"; public static final string col_10 = "day_of_month"; public static final string col_11 = "month"; sqlitedatabase mydb; int date, month; calendar c; final static string log_tag = "checkdatabase"; public databasehelper(context context) { super(context, db_name, null, 1); } @override public void oncreate(sqlitedatabase db) { db.execsql("create table " + table_name + "(" + col_1 + " integer primary key," + col_2 + " text," + col_3 + " text," + col_4 + " text," + col_5 + " text," + col_6 + " text," + col_7 + " text," + col_8 + " text," + col_9 + " text," + col_10 + " integer ," + col_11 + " integer)"); } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { db.execsql("drop table if exist " + table_name); oncreate(db); } public integer insertvalues(string title, string desc, string venue, string lat, string long, string addr, string date, string time, int dayofmonth, int monthname) { mydb = this.getwritabledatabase(); contentvalues cv = new contentvalues(); cv.put(col_2, title); cv.put(col_3, desc); cv.put(col_4, venue); cv.put(col_5, lat); cv.put(col_6, long); cv.put(col_7, addr); cv.put(col_8, date); cv.put(col_9, time); cv.put(col_10, dayofmonth); cv.put(col_11, monthname); long isinserted = mydb.insert(table_name, null, cv); if (isinserted == -1) { return 0; } else { return 1; } } public cursor getdata() { c = calendar.getinstance(); date = c.get(calendar.day_of_month); month = c.get(calendar.month); log.e(log_tag, string.valueof(date)); log.e(log_tag, string.valueof(month+1)); sqlitedatabase db = this.getwritabledatabase(); cursor res = db.rawquery("select * " + table_name + " " + col_10 + ">" + date + " , " + col_11 + ">=" + month, null); return res; } public cursor getdataexpired() { c = calendar.getinstance(); date = c.get(calendar.day_of_month); month = c.get(calendar.month); log.e(log_tag, string.valueof(date)); log.e(log_tag, string.valueof(month+1)); sqlitedatabase db = this.getwritabledatabase(); cursor res = db.rawquery("select * " + table_name + " " + col_10 + "<" + date + " , " + col_11 + "<=" + month, null); return res; } }
the values prints using log.e working fine.
Comments
Post a Comment