android - Getting the name of a table from an activity -
how create table after user has inserted title , make modifications (add, delete , update rows) in table? code line not sure about: + private string database_table = listtitle.getstringlisttitle(); have activity (listtitle) simple layout of edittext , button. want input name of table in sqlite database. activity directs activity (newlist). here code of activity:
public class listtitle extends appcompatactivity { private edittext inputlisttitle; private button buttonnext; private string stringlisttitle; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_list_title); initui(); setbuttonnext(); } private void initui() { inputlisttitle = (edittext) findviewbyid(r.id.input_list_title); buttonnext = (button) findviewbyid(r.id.button_next); } private void setbuttonnext() { buttonnext.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { if (!inputlisttitle.gettext().tostring().equals("")) { listtitle.this.stringlisttitle = inputlisttitle.gettext().tostring(); intent intent = new intent(listtitle.this, newlist.class); startactivity(intent); } else { toast noinput = toast.maketext(getapplicationcontext(), "sie müssen einen titel für die liste eintragen", toast.length_short); noinput.show(); } } }); } public string getstringlisttitle() { return stringlisttitle; } }
this activity (newlist) allows user make modofications in table named in previous activity (listtitle). here code of activity
public class newlist extends appcompatactivity { private wordpairdatabase wordpairdb; private wordpair lastentry; private textview titlelastentry; private edittext inputword, inputequivalent, lastword, lastequivalent; private button addbutton, modifybutton, deletebutton; private string stringinputword, stringinputequivalent, stringlastword, stringlastequivalent; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_new_list); initdatabase(); initui(); setaddbutton(); setlastentryui(); setdeletebutton(); setmodifybutton(); } private void initdatabase() { wordpairdb = new wordpairdatabase(this); wordpairdb.open(); } public void initui() { titlelastentry = (textview) findviewbyid(r.id.last_entry); inputword = (edittext) findviewbyid(r.id.input_word); inputequivalent = (edittext) findviewbyid(r.id.input_equivalent); lastword = (edittext) findviewbyid(r.id.last_word); lastequivalent= (edittext) findviewbyid(r.id.last_equivalent); addbutton = (button) findviewbyid(r.id.button_add); modifybutton= (button) findviewbyid(r.id.button_modify); deletebutton = (button) findviewbyid(r.id.button_delete); } public void setaddbutton() { addbutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { stringinputword = inputword.gettext().tostring(); stringinputequivalent = inputequivalent.gettext().tostring(); if (!stringinputword.equals("") && !stringinputequivalent.equals("")) { wordpairdb.addwordpair(new wordpair(stringinputword, stringinputequivalent)); setlastentryui(); inputword.settext(""); inputequivalent.settext(""); } else { toast noinput = toast.maketext(getapplicationcontext(), "sie haben nicht alles eingetragen", toast.length_short); noinput.show(); } } }); } public void setlastentryui() { lastentry = wordpairdb.retrievelast(); if (lastentry!=null) { titlelastentry.setvisibility(view.visible); lastword.setvisibility(view.visible); lastword.settext(lastentry.getword()); lastequivalent.setvisibility(view.visible); lastequivalent.settext(lastentry.getequivalent()); modifybutton.setvisibility(view.visible); deletebutton.setvisibility(view.visible); } else { titlelastentry.setvisibility(view.invisible); lastword.setvisibility(view.invisible); lastequivalent.setvisibility(view.invisible); modifybutton.setvisibility(view.invisible); deletebutton.setvisibility(view.invisible); } } public void setdeletebutton() { deletebutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { stringlastword = lastword.gettext().tostring(); stringlastequivalent = lastequivalent.gettext().tostring(); wordpairdb.deletewordpair(new wordpair(stringlastword, stringlastequivalent)); setlastentryui(); } }); } private void setmodifybutton() { modifybutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { stringlastword = lastword.gettext().tostring(); stringlastequivalent = lastequivalent.gettext().tostring(); wordpairdb.modifywordpair(new wordpair(stringlastword, stringlastequivalent)); setlastentryui(); } }); } @override protected void ondestroy() { super.ondestroy(); wordpairdb.close(); } }
and here class handle related database
public class wordpairdatabase { private static final int database_version = 1; private final string database_name = "wortlerner.db"; private final string column_id = "_id"; private final string column_word = "word"; private final string column_equivalent = "equivalent"; private string database_table = getlastcreatedtable(); private wordpairdbopenhelper dbhelper; private sqlitedatabase db; private cursor cursor; public wordpairdatabase(context context) { dbhelper = new wordpairdbopenhelper(context, database_name, null,database_version); } public string getlastcreatedtable() { return lastcreatedtable; }; public void open() throws sqlexception { try { db = dbhelper.getwritabledatabase(); } catch (sqlexception e) { db = dbhelper.getreadabledatabase(); } } public void close() {db.close();} public void addwordpair(wordpair wordpair) { contentvalues values = new contentvalues(); values.put(column_word, wordpair.getword()); values.put(column_equivalent, wordpair.getequivalent()); db.insert(database_table, null, values); } public void deletewordpair(wordpair wordpair) { string todelete = column_word + "=?"; string[] argument = new string[]{wordpair.getword()}; db.delete(database_table, todelete, argument); } public wordpair retrievelast() { string query = "select * "+ database_table +" order "+ column_id +" desc limit 1"; cursor = db.rawquery(query, null); if (cursor.movetolast()) { wordpair result = new wordpair(cursor.getstring(1), cursor.getstring(2)); cursor.close(); return result; } else { return null; } } public void modifywordpair(wordpair wordpair) { string query = "select * "+ database_table +" order "+ column_id +" desc limit 1"; cursor = db.rawquery(query, null); cursor.movetolast(); int lastid = cursor.getint(0); contentvalues values = new contentvalues(); values.put(column_word, wordpair.getword()); values.put(column_equivalent, wordpair.getequivalent()); db.update(database_table, values, column_id+"="+lastid, null); cursor.close(); } private class wordpairdbopenhelper extends sqliteopenhelper { private final string database_create = "create table " + database_table + " (" + column_id + " integer primary key autoincrement, " + column_word + " text not null, " + column_equivalent + " text not null);"; public wordpairdbopenhelper(context c, string dbname, sqlitedatabase.cursorfactory factory, int version) { super(c, dbname, factory, version); } @override public void oncreate(sqlitedatabase db) { lastcreatedtable = database_table; db.execsql(database_create); } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { db.execsql("drop table if exists " + database_table); oncreate(db); } } }
thanks in advance help
in dbopenhelper class override onconfigure method , call oncreate method again. this
public void onconfigure(sqlitedatabase db) { super.onconfigure(db); oncreate(db); }
and in oncreate method use try catch this
try { final string database_create = "create table " + database_table + " (" + column_id + " integer primary key autoincrement, " + column_word + " text not null, " + column_equivalent + " text not null);"; db.execsql(database_create); }catch (exception e){ log.e("found exception: ",e.tostring()); }
Comments
Post a Comment