android - How to update list item in sql? -
this list activity:
public class list extends actionbaractivity{ private customcursoradapter customadapter; private persondatabasehelper databasehelper; private static final int enter_data_request_code = 1; private listview listview; private static final string tag = list.class.getsimplename(); /** * called when activity first created. */ @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.listview); databasehelper = new persondatabasehelper(this); listview = (listview) findviewbyid(r.id.list_data); listview.setonitemclicklistener(new onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { log.d(tag, "clicked on item: " + position); intent intent = new intent(list.this, edit.class); person p = new person(); cursor cursor = (cursor) customadapter.getitem(position); p.name = cursor.getstring(cursor.getcolumnindex("person_name")); p.surname = cursor.getstring(cursor.getcolumnindex("person_surname")); p.enterdate = cursor.getstring(cursor.getcolumnindex("person_enterdate")); p.entertime = cursor.getstring(cursor.getcolumnindex("person_entertime")); p.exitdate = cursor.getstring(cursor.getcolumnindex("person_exitdate")); p.exittime = cursor.getstring(cursor.getcolumnindex("person_exittime")); intent.putextra("id", position); intent.putextra("name",p.name); intent.putextra("surname",p.surname ); intent.putextra("enterdate",p.enterdate ); intent.putextra("entertime",p.entertime); intent.putextra("exitdate", p.exitdate); intent.putextra("exittime", p.exittime); startactivity(intent); } }); listview.setonlongclicklistener(new onlongclicklistener() { @override public boolean onlongclick(view v) { return false; // todo auto-generated method stub } }); // database query can time consuming task .. // safe call database query in thread // handler, handle stuff <img src="http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif?m=1129645325g" alt=":)" class="wp-smiley"> new handler().post(new runnable() { @override public void run() { customadapter = new customcursoradapter(list.this, databasehelper.getalldata()); listview.setadapter(customadapter); } }); } public void onclickenterdata(view btnadd) { startactivityforresult(new intent(this, permission.class), enter_data_request_code); } public void onclicklogout(view btnlogout){ intent intent = new intent(list.this, mainactivity.class); intent.setflags(intent.flag_activity_clear_top); startactivity(intent); } @override protected void onactivityresult(int requestcode, int resultcode, intent data) { super.onactivityresult(requestcode, resultcode, data); if (resultcode == result_ok) { databasehelper.insertdata(data.getextras().getstring("tag_person_name"), data.getextras().getstring("tag_person_surname"),data.getextras().getstring("tag_person_enterdate"),data.getextras().getstring("tag_person_entertime"),data.getextras().getstring("tag_person_exitdate"),data.getextras().getstring("tag_person_exittime")); customadapter.changecursor(databasehelper.getalldata()); } }
this edit activity press on list view item :
public class edit extends actionbaractivity { edittext name; edittext surname; edittext date; edittext time; edittext edate; edittext etime; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_edit); intent intent = getintent(); string data_name = intent.getstringextra("name"); string data_surname = intent.getstringextra("surname"); string data_enterdate= intent.getstringextra("enterdate"); string data_entertime = intent.getstringextra("entertime"); string data_exitdate = intent.getstringextra("exitdate"); string data_exittime = intent.getstringextra("exittime"); //typo here name = (edittext) findviewbyid(r.id.username); //corrected surname = (edittext) findviewbyid(r.id.usersurname); //corrected date = (edittext) findviewbyid(r.id.date2); time = (edittext) findviewbyid(r.id.time2); edate = (edittext) findviewbyid(r.id.date3); etime = (edittext) findviewbyid(r.id.time3); name.settext(data_name); surname.settext(data_surname); date.settext(data_enterdate); time.settext(data_entertime); edate.settext(data_exitdate); etime.settext(data_exittime); } public void oncancel(view btncancel){ finish(); } public void onclicksave(view btnsave){ } } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.edit, menu); return true; } @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(); if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); } }
so in edit activity defined button public onclicksave(view btnsave), want make database upgrade after press button, after lot of tries, managed upgrade database wont update clciked item in list, creates new (one more item) in list. starting beginning again :( .
this databasehelper:
public class persondatabasehelper { private static final string tag = persondatabasehelper.class.getsimplename(); // database configuration // if want onupgrade run change database_version private static final int database_version = 7; private static final string database_name = "database.db"; // table configuration static final string table_name = "person_table"; // table name static final string person_table_column_id = "_id"; // column named "_id" required cursor static final string person_table_column_name = "person_name"; static final string person_table_column_surname = "person_surname"; static final string person_table_column_enterdate = "person_enterdate"; static final string person_table_column_entertime = "person_entertime"; static final string person_table_column_exitdate = "person_exitdate"; static final string person_table_column_exittime = "person_exittime"; private databaseopenhelper openhelper; private sqlitedatabase database; // wrapper class. means, outside world, communicate persondatabasehelper, // under hood databaseopenhelper class perform database crud operations public persondatabasehelper(context acontext) { openhelper = new databaseopenhelper(acontext); database = openhelper.getwritabledatabase(); } public void insertdata (string apersonname, string apersonsurname, string apersonenterdate,string apersonentertime, string apersonexitdate,string apersonexittime) { // using contentvalues avoid sql format errors contentvalues contentvalues = new contentvalues(); contentvalues.put(person_table_column_name, apersonname); contentvalues.put(person_table_column_surname, apersonsurname); contentvalues.put(person_table_column_enterdate, apersonenterdate); contentvalues.put(person_table_column_entertime, apersonentertime); contentvalues.put(person_table_column_exitdate, apersonexitdate); contentvalues.put(person_table_column_exittime, apersonexittime); database.insert(table_name, null, contentvalues); } public cursor getalldata () { string buildsql = "select * " + table_name; log.d(tag, "getalldata sql: " + buildsql); return database.rawquery(buildsql, null); } // databaseopenhelper class used perform database related operation private class databaseopenhelper extends sqliteopenhelper { public databaseopenhelper(context acontext) { super(acontext, database_name, null, database_version); } @override public void oncreate(sqlitedatabase sqlitedatabase) { // create tables here string buildsql = "create table " + table_name + "( " + person_table_column_id + " integer primary key, " + person_table_column_name + " text, " + person_table_column_surname + " text, " + person_table_column_enterdate + " text," + person_table_column_entertime + " text," + person_table_column_exitdate + " text," + person_table_column_exittime + " text )"; log.d(tag, "oncreate sql: " + buildsql); sqlitedatabase.execsql(buildsql); } @override public void onupgrade(sqlitedatabase sqlitedatabase, int oldversion, int newversion) { // database schema upgrade code goes here string buildsql = "drop table if exists " + table_name; log.d(tag, "onupgrade sql: " + buildsql); sqlitedatabase.execsql(buildsql); // drop previous table oncreate(sqlitedatabase); // create table beginning } } }
this custom cursor:
public class customcursoradapter extends cursoradapter { @suppresswarnings("deprecation") public customcursoradapter(context context, cursor c) { super(context, c); } @override public view newview(context context, cursor cursor, viewgroup parent) { // when view created first time, // need tell adapters, how each item layoutinflater inflater = layoutinflater.from(parent.getcontext()); view retview = inflater.inflate(r.layout.single_row_item, parent, false); return retview; } @override public void bindview(view view, context context, cursor cursor) { // here setting our data // means, take data cursor , put in views textview textviewpersonname = (textview) view.findviewbyid(r.id.name); textviewpersonname.settext(cursor.getstring(cursor.getcolumnindex(cursor.getcolumnname(1)))); textview textviewpersonsurname = (textview) view.findviewbyid(r.id.surname); textviewpersonsurname.settext(cursor.getstring(cursor.getcolumnindex(cursor.getcolumnname(2)))); textview textviewpersonenterdate = (textview) view.findviewbyid(r.id.date); textviewpersonenterdate.settext(cursor.getstring(cursor.getcolumnindex(cursor.getcolumnname(3)))); textview textviewpersonentertime = (textview) view.findviewbyid(r.id.time); textviewpersonentertime.settext(cursor.getstring(cursor.getcolumnindex(cursor.getcolumnname(4)))); textview textviewpersonexitdate = (textview) view.findviewbyid(r.id.date2); textviewpersonexitdate.settext(cursor.getstring(cursor.getcolumnindex(cursor.getcolumnname(5)))); textview textviewpersonexittime = (textview) view.findviewbyid(r.id.time2); textviewpersonexittime.settext(cursor.getstring(cursor.getcolumnindex(cursor.getcolumnname(6)))); } }
and person class :
public class person { string name; string surname; string enterdate; string entertime; string exitdate; string exittime; }
this list
this edit activity press on item :
so , there u can see add button updates info.
read tutorial. in particular, take @ section 9, can find info contentproviders, loaders , cursoradapter. example shows how use simplecursoradapter, can modify use cursoradapter; in case, if follow tutorial have clean code , database notifies automatically ui when insert, update or delete performed against database.
Comments
Post a Comment