Android SQLite: Failing to update records -
i'm developing small app keeps records of places , categories belong to. now, i'm working on category table , once manage perform crud operations, i'll advance rest of app.
problem is, managed insert, delete , read table can't update. i've got code:
dbadapter:
... public boolean updatecategory(long rowid, string name) { contentvalues args = new contentvalues(); args.put(key_id_cat, rowid); args.put(key_name_cat, name); return db.update(table_category, args, key_rowid_cat + "=" + rowid, null) > 0; } ...
my editcategory class:
public class s7_editcategory extends activity { edittext et_name_category; private context ctx; int duration = toast.length_short; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.s7_edit_categories); et_name_category = (edittext) findviewbyid(r.id.edit_category); ctx = this; final string nome = getintent().getstringextra("nome"); final long id_cat = getintent().getintextra("_id", 0); et_nome_category.settext(nome); button button_save = (button) findviewbyid(r.id.button3); button_save.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { try { string new_cat = et_name_category.gettext().tostring(); dbadapter dbadap = dbadapter.getinstance(ctx); dbadap.updatecategory(id_cat, new_cat); intent = new intent(ctx, s5_categories.class); setresult(result_ok, i); finish(); } catch (exception ex) { throw new error("some error"); } } }); } }
logcat tells me ( _id = 0 ) so, think has parameter passed on previous activity, category:
public class s5_categories extends listactivity { private context ctx; private simplecursoradapter adapter; public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.s5_categorias); ctx = this; filllist(); getlistview().setonitemclicklistener(new onitemclicklistener() { @override public void onitemclick(adapterview<?> parentview, view view, int position, long id) { cursor c = (cursor) parentview.getitematposition(position); intent = new intent(ctx, s7_editcategoria.class); i.putextra("nome", c.getstring(c.getcolumnindexorthrow("nome"))); i.putextra("id_cat", c.getcolumnindex("_id") + 1); startactivityforresult(i, 0); } }); } private void filllist() { try { dbadapter dbadap = dbadapter.getinstance(ctx); cursor c = dbadap.getallcategorias(); string[] = new string[] { "nome" }; int[] = new int[] { android.r.id.text1 }; adapter = new simplecursoradapter(this, android.r.layout.simple_list_item_1, c, from, to, simplecursoradapter.flag_register_content_observer); adapter.setdropdownviewresource(android.r.layout.simple_spinner_dropdown_item); setlistadapter(adapter); } catch (exception ex) { throw new error(ex.getmessage()); } } @override protected void onactivityresult(int requestcode, int resultcode, intent data) { super.onactivityresult(requestcode, resultcode, data); switch(requestcode) { case 0: if (data != null) { dbadapter dbadap = dbadapter.getinstance(ctx); cursor cursor = dbadap.getallcategorias(); adapter.changecursor(cursor); } } } }
well, can me please? in advance chiapa
ok, managed parameter id, still errors when updating: try, error like:
error updating name=hospitals id_cat=4 using update category set name=?, id_cat=? id=4
please help
chiapa
try following way
db.update(table_category, args, key_rowid_cat + "=?", new string[] { (rowid) });
instead of
db.update(table_category, args, key_rowid_cat + "=" + rowid, null)
why want int
. valid reason. didn,t find reason code provided. if no valid reason means change method following.
public boolean updatecategory(long rowid, string name) { contentvalues args = new contentvalues(); args.put(key_id_cat, rowid); args.put(key_name_cat, name); db.update(table_category, args, key_rowid_cat + "=?", new string[] { (rowid) }); }
i hope you.
Comments
Post a Comment