android - Pass data from SQLite to RecyclerView -
i want pass data sqlite db recyclerview, unfortunately app crashes, , don't know how proceed achieve goal.
moment, app shows elements in db inside alertdialog, want display them in recyclerview.
here's code:
the following activity user inserts data:
public class inserimentoscadenze extends appcompatactivity { //dichiaro il db dbhelperadapter.scadenzedbhelper mydb; edittext et1, et2, et3; button btnaddinfo, btnviewinfo; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_inserimento_scadenze); /**lo istanzio, così mi richiama il costruttore nella classe * del dbhelper così da poter creare le tabelle */ mydb = new dbhelperadapter.scadenzedbhelper(this); et1 = (edittext)findviewbyid(r.id.edittexttipo); et2 = (edittext)findviewbyid(r.id.edittextnote); et3 = (edittext)findviewbyid(r.id.edittextdata); btnaddinfo = (button)findviewbyid(r.id.btnadd ); btnviewinfo = (button)findviewbyid(r.id.btnview); addinfo(); viewall(); } public void addinfo(){ btnaddinfo.setonclicklistener( new view.onclicklistener() { @override public void onclick(view v) { boolean isinserted = mydb.insertinfo(et1.gettext().tostring(), et2.gettext().tostring(), et3.gettext().tostring()); if(isinserted == true){ toast.maketext(inserimentoscadenze.this, "le informazioni sono state inserite", toast.length_long).show(); et1.settext(""); et2.settext(""); et3.settext(""); } else { toast.maketext(inserimentoscadenze.this, "errore durante l'inserimento" , toast.length_long).show(); et1.settext(""); et2.settext(""); et3.settext(""); } } } ); } public void viewall(){ btnviewinfo.setonclicklistener( new view.onclicklistener() { @override public void onclick(view v) { cursor result = mydb.getallinfo(); if(result.getcount() == 0){ //mostra un messaggio showmessage("errore","nessun risultato trovato"); return; } stringbuffer buffer = new stringbuffer(); while (result.movetonext()){ buffer.append("id :"+ result.getstring(0)+"\n"); buffer.append("tipo :"+ result.getstring(1)+"\n"); buffer.append("note :"+ result.getstring(2)+"\n"); buffer.append("data :"+ result.getstring(3)+"\n\n "); } showmessage("info",buffer.tostring() ); } } ); } public void showmessage(string title, string message) { alertdialog.builder builder = new alertdialog.builder(this); builder.setcancelable(true); builder.settitle(title); builder.setmessage(message); builder.show(); } public void populatelistview(){ cursor cursor = mydb.getallinfo(); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.menu_main, menu); return true; } @override public void onbackpressed() { // code. startactivity(new intent(this, mainactivity.class)); } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); //noinspection simplifiableifstatement if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); }}
this dbhelperadapter:
public class dbhelperadapter { static scadenzedbhelper helper; public dbhelperadapter(context context) { helper = new scadenzedbhelper(context); } public long insetdata(string tipo, string note, string date) { sqlitedatabase db = helper.getwritabledatabase(); contentvalues contentvalues = new contentvalues(); contentvalues.put(scadenzedbhelper.col_2, tipo); contentvalues.put(scadenzedbhelper.col_3, note); contentvalues.put(scadenzedbhelper.col_4, date); long id = db.insert(scadenzedbhelper.table_name, null, contentvalues); db.close(); return id; } public list<cardinfo> getalldata() { sqlitedatabase db = helper.getwritabledatabase(); list<cardinfo> list = new arraylist<>(); string[] columns = {scadenzedbhelper.col_1, scadenzedbhelper.col_2, scadenzedbhelper.col_3, scadenzedbhelper.col_4}; cursor cursor = db.query(scadenzedbhelper.table_name, columns, null, null, null, null, null); stringbuffer buffer = new stringbuffer(); if(cursor != null){ while (cursor.movetonext()) { int cid = cursor.getint(cursor.getcolumnindex(scadenzedbhelper.col_1)); string tipo = cursor.getstring(cursor.getcolumnindex(scadenzedbhelper.col_2)); string note = cursor.getstring(cursor.getcolumnindex(scadenzedbhelper.col_3)); string date = cursor.getstring(cursor.getcolumnindex(scadenzedbhelper.col_4)); buffer.append(cid + " " + tipo + " " + note + " " + date + "\n"); } } return list; } public static list<cardinfo> getalldata_a() { sqlitedatabase db = helper.getwritabledatabase(); string[] columns = {scadenzedbhelper.col_1, scadenzedbhelper.col_2, scadenzedbhelper.col_3, scadenzedbhelper.col_4}; cursor cursor = db.query(scadenzedbhelper.table_name, columns, null, null, null, null, null); list<cardinfo> data = new arraylist<>(); if(cursor != null){ while (cursor.movetonext()) { int cid = cursor.getint(cursor.getcolumnindex(scadenzedbhelper.col_1)); string tipo = cursor.getstring(cursor.getcolumnindex(scadenzedbhelper.col_2)); string note = cursor.getstring(cursor.getcolumnindex(scadenzedbhelper.col_3)); string date = cursor.getstring(cursor.getcolumnindex(scadenzedbhelper.col_4)); cardinfo current = new cardinfo(); current.id = cid; current.tipo = tipo; current.note = note; current.date = date; data.add(current); } } return data; } public static string getdata(string tipo) { sqlitedatabase db = helper.getwritabledatabase(); string[] columns = {scadenzedbhelper.col_2, scadenzedbhelper.col_3, scadenzedbhelper.col_4}; cursor cursor = db.query(scadenzedbhelper.table_name, columns, scadenzedbhelper.col_2 + " = '" + tipo + "' ", null, null, null, null); stringbuffer buffer = new stringbuffer(); if(cursor != null){ while (cursor.movetonext()) { string tiponota = cursor.getstring(cursor.getcolumnindex(scadenzedbhelper.col_2)); string nota = cursor.getstring(cursor.getcolumnindex(scadenzedbhelper.col_3)); string date = cursor.getstring(cursor.getcolumnindex(scadenzedbhelper.col_4)); buffer.append(tiponota + " " + nota + " " + date + "\n"); } } return buffer.tostring(); } static class scadenzedbhelper extends sqliteopenhelper { public static final string database_name = "scadenze.db"; public static final string table_name = "scadenze_table"; public static final string col_1 = "id"; public static final string col_2 = "tipo"; public static final string col_3 = "note"; public static final string col_4 = "data"; private static final string create_table="create table \" + table_name + \"(id integer primary key autoincrement, tipo text, note text, data text) \""; private context context; public scadenzedbhelper(context context) { super(context, database_name, null, 1); sqlitedatabase db = this.getwritabledatabase(); } @override public void oncreate(sqlitedatabase db) { /** * il metodo qui immediatamente sotto esegue qualsiasi query gli si passi * come parametro, accetta una string, o anche string query */ db.execsql(create_table); } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { db.execsql("drop table if exists " + table_name); oncreate(db); } public boolean insertinfo(string tipo, string note, string data) { //costruttore sqlitedatabase db = this.getwritabledatabase(); contentvalues contentvalues = new contentvalues(); contentvalues.put(col_2, tipo); contentvalues.put(col_3, note); contentvalues.put(col_4, data); long result = db.insert(table_name, null, contentvalues); if (result == -1) { return false; } else { return true; } } public cursor getallinfo() { //costruttore sqlitedatabase db = this.getwritabledatabase(); cursor result = db.rawquery("select * " + table_name, null); return result; } }}
and adapter class:
public class dbhelperadapter { static scadenzedbhelper helper; public dbhelperadapter(context context) { helper = new scadenzedbhelper(context); } public long insetdata(string tipo, string note, string date) { sqlitedatabase db = helper.getwritabledatabase(); contentvalues contentvalues = new contentvalues(); contentvalues.put(scadenzedbhelper.col_2, tipo); contentvalues.put(scadenzedbhelper.col_3, note); contentvalues.put(scadenzedbhelper.col_4, date); long id = db.insert(scadenzedbhelper.table_name, null, contentvalues); db.close(); return id; } public list<cardinfo> getalldata() { sqlitedatabase db = helper.getwritabledatabase(); list<cardinfo> list = new arraylist<>(); string[] columns = {scadenzedbhelper.col_1, scadenzedbhelper.col_2, scadenzedbhelper.col_3, scadenzedbhelper.col_4}; cursor cursor = db.query(scadenzedbhelper.table_name, columns, null, null, null, null, null); stringbuffer buffer = new stringbuffer(); if(cursor != null){ while (cursor.movetonext()) { int cid = cursor.getint(cursor.getcolumnindex(scadenzedbhelper.col_1)); string tipo = cursor.getstring(cursor.getcolumnindex(scadenzedbhelper.col_2)); string note = cursor.getstring(cursor.getcolumnindex(scadenzedbhelper.col_3)); string date = cursor.getstring(cursor.getcolumnindex(scadenzedbhelper.col_4)); buffer.append(cid + " " + tipo + " " + note + " " + date + "\n"); } } return list; } public static list<cardinfo> getalldata_a() { sqlitedatabase db = helper.getwritabledatabase(); string[] columns = {scadenzedbhelper.col_1, scadenzedbhelper.col_2, scadenzedbhelper.col_3, scadenzedbhelper.col_4}; cursor cursor = db.query(scadenzedbhelper.table_name, columns, null, null, null, null, null); list<cardinfo> data = new arraylist<>(); if(cursor != null){ while (cursor.movetonext()) { int cid = cursor.getint(cursor.getcolumnindex(scadenzedbhelper.col_1)); string tipo = cursor.getstring(cursor.getcolumnindex(scadenzedbhelper.col_2)); string note = cursor.getstring(cursor.getcolumnindex(scadenzedbhelper.col_3)); string date = cursor.getstring(cursor.getcolumnindex(scadenzedbhelper.col_4)); cardinfo current = new cardinfo(); current.id = cid; current.tipo = tipo; current.note = note; current.date = date; data.add(current); } } return data; } public static string getdata(string tipo) { sqlitedatabase db = helper.getwritabledatabase(); string[] columns = {scadenzedbhelper.col_2, scadenzedbhelper.col_3, scadenzedbhelper.col_4}; cursor cursor = db.query(scadenzedbhelper.table_name, columns, scadenzedbhelper.col_2 + " = '" + tipo + "' ", null, null, null, null); stringbuffer buffer = new stringbuffer(); if(cursor != null){ while (cursor.movetonext()) { string tiponota = cursor.getstring(cursor.getcolumnindex(scadenzedbhelper.col_2)); string nota = cursor.getstring(cursor.getcolumnindex(scadenzedbhelper.col_3)); string date = cursor.getstring(cursor.getcolumnindex(scadenzedbhelper.col_4)); buffer.append(tiponota + " " + nota + " " + date + "\n"); } } return buffer.tostring(); } static class scadenzedbhelper extends sqliteopenhelper { public static final string database_name = "scadenze.db"; public static final string table_name = "scadenze_table"; public static final string col_1 = "id"; public static final string col_2 = "tipo"; public static final string col_3 = "note"; public static final string col_4 = "data"; private static final string create_table="create table \" + table_name + \"(id integer primary key autoincrement, tipo text, note text, data text) \""; private context context; public scadenzedbhelper(context context) { super(context, database_name, null, 1); sqlitedatabase db = this.getwritabledatabase(); } @override public void oncreate(sqlitedatabase db) { /** * il metodo qui immediatamente sotto esegue qualsiasi query gli si passi * come parametro, accetta una string, o anche string query */ db.execsql(create_table); } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { db.execsql("drop table if exists " + table_name); oncreate(db); } public boolean insertinfo(string tipo, string note, string data) { //costruttore sqlitedatabase db = this.getwritabledatabase(); contentvalues contentvalues = new contentvalues(); contentvalues.put(col_2, tipo); contentvalues.put(col_3, note); contentvalues.put(col_4, data); long result = db.insert(table_name, null, contentvalues); if (result == -1) { return false; } else { return true; } } public cursor getallinfo() { //costruttore sqlitedatabase db = this.getwritabledatabase(); cursor result = db.rawquery("select * " + table_name, null); return result; } }}
this logcat of crash:
java.lang.runtimeexception: unable start activity componentinfo{it.alessandrocucci.cardrecyclerview/it.alessandrocucci.cardrecyclerview.inserimentoscadenze}: android.database.sqlite.sqliteexception: unrecognized token: """ (code 1): , while compiling: create table " + table_name + "(id integer primary key autoincrement, tipo text, note text, data text) " @ android.app.activitythread.performlaunchactivity(activitythread.java:2298) @ android.app.activitythread.handlelaunchactivity(activitythread.java:2360) @ android.app.activitythread.access$800(activitythread.java:144) @ android.app.activitythread$h.handlemessage(activitythread.java:1278) @ android.os.handler.dispatchmessage(handler.java:102) @ android.os.looper.loop(looper.java:135) @ android.app.activitythread.main(activitythread.java:5221) @ java.lang.reflect.method.invoke(native method) @ java.lang.reflect.method.invoke(method.java:372) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:899) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:694) caused by: android.database.sqlite.sqliteexception: unrecognized token: """ (code 1): , while compiling: create table " + table_name + "(id integer primary key autoincrement, tipo text, note text, data text) " @ android.database.sqlite.sqliteconnection.nativepreparestatement(native method) @ android.database.sqlite.sqliteconnection.acquirepreparedstatement(sqliteconnection.java:889) @ android.database.sqlite.sqliteconnection.prepare(sqliteconnection.java:500) @ android.database.sqlite.sqlitesession.prepare(sqlitesession.java:58
the error in table creation:
private static final string create_table="create table \" + table_name + \"(id integer primary key autoincrement, tipo text, note text, data text) \"";
should be
private static final string create_table="create table " + table_name + " (id integer primary key autoincrement, tipo text, note text, data text)";
you used escape characters in randomic way.
Comments
Post a Comment