database - I am trying to display data from rows by date column in android app -
i writing edittext , textview database in app. want display added specific column; have date_selected. having major block , appreciated. below code section:
public class calendardbadapter { public static final string key_title = "title"; public static final string date_selected = "date"; public static final string weight_input = "weight"; public static final string reps_input = "reps"; public static final string key_body = "body"; public static final string key_rowid = "id"; private static final string tag = "calendardbadapter"; private databasehelper mydbhelper; private sqlitedatabase mydatabase; private static final string database_name = "exerciselog"; private static final string database_table = "exercises"; private static final int database_version = 1; //database creation sql statement private static final string database_create = "create table " + database_table + "(" + key_rowid + " integer primary key autoincrement, " + key_title + date_selected + weight_input + reps_input + " text not null);"; private final context mctx; private static class databasehelper extends sqliteopenhelper { databasehelper(context context) { super(context, database_name, null, database_version); } @override public void oncreate(sqlitedatabase db) { try { db.execsql(database_create); } catch (sqlexception e) { e.printstacktrace(); } } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { log.w(tag, "upgrading database version " + oldversion + " " + newversion + ", destroy old data"); db.execsql("drop tsble if exists exercises"); oncreate(db); } } //constructor public calendardbadapter(context ctx) { this.mctx = ctx; } /* * open exercises database. if cannot opened, try create new instance of database * if cannot created, trow , exception signal failure * @return (self reference, allowing chained in * initialization call) * @throws sqlexception if database neither opened or created */ public calendardbadapter open()throws sqlexception { mydbhelper = new databasehelper(mctx); mydatabase = mydbhelper.getwritabledatabase(); return this; } public void close() { mydbhelper.close(); } /* * create new exercise using title provided. if note * created return new rowid exercise, otherwise return * -1 indicate failure. * * @param title title of exercise * @return rowid or -1 if failed */ public long createexercise(string title, string date, string weight, string reps, string body) { contentvalues initialvalues = new contentvalues(); initialvalues.put(key_title, title); initialvalues.put(date_selected, date); initialvalues.put(weight_input, weight); initialvalues.put(reps_input, reps); initialvalues.put(key_body, body); return mydatabase.insert(database_table, null, initialvalues); } /* * delete exercise given rowid * @param rowid id of exercise delete * @return true if deleted, false otherwise */ public boolean deleteexercise(long rowid) { return mydatabase.delete(database_table, key_rowid + "=" + rowid, null) > 0; } /** * return cursor on list of exercise inputs in database * * @return cursor on exercise inputs */ public cursor fetchallexercises() { return mydatabase.query(database_table, new string[] {key_rowid, key_title, date_selected, weight_input, reps_input, key_body}, null, null, null, null, null); } /** * return cursor positioned @ exercise matches given rowid * * @param rowid id of exercise retrieve * @return cursor positioned matching note, if found * @throws sqlexception if note not found/retrieved */ public cursor fetchexerciss(long rowid)throws sqlexception { cursor mycursor = mydatabase.query(true, database_table, new string[] {key_rowid, key_title, date_selected, weight_input, reps_input, key_body}, key_rowid + "=" + rowid, null, null, null, null, null); if(mycursor != null) { mycursor.movetofirst(); } return mycursor; } /** * update exercises using details provided. note updated * specified using rowid, , altered use title values passed in * * @param rowid id of exercise update * @param title value set exercise title * @param body value set exercise body * @return true if exercise updated, false otherwise */ public boolean updateexercise(long rowid, string title, string date, string weight, string reps, string body) { contentvalues args = new contentvalues(); args.put(key_title, title); args.put(date_selected, date); args.put(weight_input, weight); args.put(reps_input, reps); args.put(key_body, body); return mydatabase.update(database_table, args, key_rowid + "=" + rowid, null) > 0; } public cursor sortallrows() { return mydatabase.query(database_table, new string[] {key_rowid, key_title, date_selected, weight_input, reps_input, key_body}, null, null, null, null, date_selected + " asc"); } }
activity:
package com.mobifit; import java.util.calendar; import java.util.date; import java.util.logging.simpleformatter; import com.mobifit.r.string; import android.os.bundle; import android.app.activity; import android.content.intent; import android.database.cursor; import android.text.format.dateformat; import android.view.menu; import android.view.view; import android.widget.button; import android.widget.edittext; import android.widget.textview; public class addtocalendar extends activity { private button favbtn; private button timerbtn; private button editbtn; private button selecteddmy; private button addcalbtn; private textview exercise, exercisedb, weighttext, repstext, notestext; private string textpassed, datepassed; private edittext weightinput, repsinput, notesinput; private long myrowid; private calendardbadapter mydbhelper; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); mydbhelper = new calendardbadapter(this); mydbhelper.open(); setcontentview(r.layout.add_to_calendar); //set view xml favbtn = (button)this.findviewbyid(r.id.favoritebtn); timerbtn = (button)this.findviewbyid(r.id.timerbtn); editbtn = (button)this.findviewbyid(r.id.editbtn); addcalbtn = (button)this.findviewbyid(r.id.addtocalbtn); weightinput = (edittext)this.findviewbyid(r.id.weightinput); repsinput = (edittext)this.findviewbyid(r.id.repsinput); notesinput = (edittext)this.findviewbyid(r.id.notesinput); exercisedb = (textview)this.findviewbyid(r.id.exercisedb); weighttext = (textview)this.findviewbyid(r.id.weight); repstext = (textview)this.findviewbyid(r.id.repstext); notestext = (textview)this.findviewbyid(r.id.notestext); //set date selecteddmy = (button)this.findviewbyid(r.id.selecteddaymonthyear); datepassed = globalvars.getdate(); if(datepassed == null) { calendar cal = calendar.getinstance(); int day = cal.get(calendar.day_of_month); int month = cal.get(calendar.month); int year = cal.get(calendar.year); integer.tostring(day); integer.tostring(month); integer.tostring(year); selecteddmy.settext("today" + month + "-" + day + "-" + year + "\n(click change)"); } else { selecteddmy.settext(datepassed + "\n(click change)"); } exercise = (textview)this.findviewbyid(r.id.exercise); //enable button favbtn.setenabled(true); timerbtn.setenabled(true); editbtn.setenabled(true); selecteddmy.setenabled(true); //set text exercise passed textpassed = globalvars.getvar(); exercise.settext(textpassed); //rowid database myrowid = (savedinstancestate == null) ? null: (long)savedinstancestate.getserializable(calendardbadapter.key_rowid); if(myrowid == null) { bundle extras = getintent().getextras(); myrowid = extras != null ? extras.getlong(calendardbadapter.key_rowid): null; } //populatefields(); addcalbtn.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { //previous code /*setresult(result_ok); finish();*/ } }); } public void dateclick(view v) { startactivity(new intent(this, mobifitcalendar.class)); } //add data calendar display public void addtocalbtnclick() { } private void populatefields() { if(myrowid != null) { cursor exerciseinput = mydbhelper.fetchexerciss(myrowid); startmanagingcursor(exerciseinput); weightinput.settext(exerciseinput.getstring(exerciseinput.getcolumnindexorthrow(calendardbadapter.weight_input))); repsinput.settext(exerciseinput.getstring(exerciseinput.getcolumnindexorthrow(calendardbadapter.reps_input))); notesinput.settext(exerciseinput.getstring(exerciseinput.getcolumnindexorthrow(calendardbadapter.key_body))); } } @override protected void onsaveinstancestate(bundle outstate) { super.onsaveinstancestate(outstate); savestate(); outstate.putserializable(calendardbadapter.key_rowid, myrowid); } @override protected void onpause() { super.onpause(); savestate(); } @override protected void onresume() { super.onresume(); populatefields(); } private void savestate() { string title = exercise.tostring(); string weight = weightinput.gettext().tostring(); string reps = weightinput.gettext().tostring(); string notes = notesinput.gettext().tostring(); string dateselected = selecteddmy.tostring(); if(myrowid == null) { long id = mydbhelper.createexercise(title, dateselected, weight, reps, notes); if(id > 0) { myrowid = id; } } else { mydbhelper.updateexercise(myrowid, title, dateselected, weight, reps, notes); } } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.add_to_calendar, menu); return true; } }
Comments
Post a Comment