Add and Retrieve data from SQLite Database Android in listview -
ok i'm new here , know question has been asked many times cant seem work can samples i've found work fine in listview if try modify or recreate cant seem work im curreouse doing wrong code....sorry in advance bad formatting.
public class orgeventcreationactivity extends activity { private sqliteadapter mysqliteadapter; public edittext eventnames; public edittext evedes; public edittext numofv; public edittext possisions; public edittext skills; public edittext minage; public button saveevent; public button proceed; public button stime; public button etime; public button date; public listview list; public button submit; /** called when activity first created. */ @suppresswarnings("deprecation") @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_org_event_creation); eventnames = (edittext)findviewbyid(r.id.eventnameinput); evedes = (edittext)findviewbyid (r.id.eventdescriptioninput); numofv = (edittext)findviewbyid (r.id.numofv); possisions = (edittext)findviewbyid (r.id.posinput); skills = (edittext)findviewbyid (r.id.skillsinput); minage = (edittext)findviewbyid (r.id.minage); saveevent =(button)findviewbyid (r.id.aeventbtn); proceed = (button)findviewbyid (r.id.submitbtn); stime = (button)findviewbyid (r.id.stimebtn); etime = (button)findviewbyid (r.id.etimebtn); date = (button)findviewbyid (r.id.datepicker); list = (listview)findviewbyid (r.id.orglist); submit = (button)findviewbyid (r.id.submitbtn); submit.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub intent = new intent (orgeventcreationactivity.this,userhomeactivity.class); startactivity(us); } }); saveevent.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub populatelist(); } }); } public void populatelist(){ string evename = eventnames.gettext().tostring().trim(); /* * create/open sqlite database * , fill dummy content * , close */ mysqliteadapter = new sqliteadapter(this); mysqliteadapter.opentowrite(); mysqliteadapter.deleteall(); mysqliteadapter.insert(evename); mysqliteadapter.close(); intent log = new intent(orgeventcreationactivity.this, userhomeactivity.class); startactivity(log); } mysqliteadapter = new sqliteadapter(this); mysqliteadapter.opentoread(); cursor cursor = mysqliteadapter.queueall(); startmanagingcursor(cursor); string[] = new string[]{sqliteadapter.mydatabase_table}; int[] = new int[]{r.id.text}; simplecursoradapter cursoradapter = new simplecursoradapter(this, r.layout.row, cursor, from, to); listcontent.setadapter(cursoradapter); mysqliteadapter.close(); } } }
the activity display listview
import android.app.activity; import android.database.cursor; import android.os.bundle; import android.widget.listview; import android.widget.simplecursoradapter; public class userhomeactivity extends activity { private sqliteadapter mysqliteadapter; /** called when activity first created. */ @suppresswarnings("deprecation") @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); listview listcontent = (listview)findviewbyid(r.id.contentlist); /* * create/open sqlite database * , fill dummy content * , close * * mysqliteadapter = new sqliteadapter(this); *mysqliteadapter.opentowrite(); * mysqliteadapter.deleteall(); *mysqliteadapter.insert("charity dinner"); *mysqliteadapter.insert("fundraiser"); *mysqliteadapter.insert("benifit concert"); *mysqliteadapter.insert("silent auction"); * *mysqliteadapter.close(); */ /* * open same sqlite database * , read it's content. */ mysqliteadapter = new sqliteadapter(this); mysqliteadapter.opentoread(); cursor cursor = mysqliteadapter.queueall(); startmanagingcursor(cursor); string[] = new string[]{sqliteadapter.mydatabase_table}; int[] = new int[]{r.id.text}; simplecursoradapter cursoradapter = new simplecursoradapter(this, r.layout.row, cursor, from, to); listcontent.setadapter(cursoradapter); mysqliteadapter.close(); } }
and database
import android.content.contentvalues; import android.content.context; import android.database.cursor; import android.database.sqlite.sqlitedatabase; import android.database.sqlite.sqliteopenhelper; import android.database.sqlite.sqlitedatabase.cursorfactory; import android.view.view.onclicklistener; import android.content.contentvalues; import android.content.context; import android.database.cursor; import android.database.sqlite.sqlitedatabase; import android.database.sqlite.sqliteopenhelper; import android.database.sqlite.sqlitedatabase.cursorfactory; public class sqliteadapter { public static final string mydatabase_name = "my_database"; public static final string mydatabase_table = "my_table"; public static final int mydatabase_version = 1; public static final string name_column = "events"; public static final string key_id = "_id"; public static final string key_content = "events"; //create table my_database (id integer primary key, content text not null); private static final string script_create_database = "create table " + mydatabase_table + " (" + key_id + " integer primary key aautoincrement,"+ "evnents text);"; private sqlitehelper sqlitehelper; private sqlitedatabase sqlitedatabase; private context context; public sqliteadapter(context c){ context = c; } public sqliteadapter opentoread() throws android.database.sqlexception { sqlitehelper = new sqlitehelper(context, mydatabase_name, null, mydatabase_version); sqlitedatabase = sqlitehelper.getreadabledatabase(); return this; } public sqliteadapter opentowrite() throws android.database.sqlexception { sqlitehelper = new sqlitehelper(context, mydatabase_name, null, mydatabase_version); sqlitedatabase = sqlitehelper.getwritabledatabase(); return this; } public void close(){ sqlitehelper.close(); } public void insertentry(string evename) { contentvalues newvalues = new contentvalues(); // assign values each row. newvalues.put("evets",evename); // insert row table sqlitedatabase.insert("ev", null, newvalues); ///toast.maketext(context, "reminder saved", toast.length_long).show(); } public int deleteall(){ return sqlitedatabase.delete(mydatabase_table, null, null); } public cursor queueall(){ string[] columns = new string[]{key_id, key_content}; cursor cursor = sqlitedatabase.query(mydatabase_table, columns, name_column, null, null, null, null); return cursor; } public class sqlitehelper extends sqliteopenhelper { public sqlitehelper(context context, string name, cursorfactory factory, int version) { super(context, name, factory, version); } @override public void oncreate(sqlitedatabase db) { // todo auto-generated method stub db.execsql(script_create_database); } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { // todo auto-generated method stub } } }
i've been trying figure out days if can greatful
database recreate in 2 way.
- upgrade db version in sqliteadapter.
- remove db file device or emulator. if it's real device, clear data in settings.
Comments
Post a Comment