java - SQlite delete function is not deleting entries -
im having trouble getting individual items delete in database application using. know method gets called, nothing in list ever removed. im not getting errors making tough track down. assistance awesome.
public class mainactivity extends activity{ //global variables listview lv; intent addm, viewm; public dbadapter moviedatabase; string temptitle, tempyear; int request_code = 1; int request_code2 = 2; simplecursoradapter dataadapter; cursor cursor; button addbutton; long testid; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); //creates database moviedatabase = new dbadapter(this); moviedatabase.open(); //moviedatabase.deleteallmovies(); //creates intents start sub activities addm = new intent(this, addmovie.class); viewm = new intent(this, movieview.class); } //handles return of activity addmovie public void onactivityresult(int requestcode, int resultcode, intent data) { super.onactivityresult(requestcode, resultcode, data); if(resultcode == result_ok) { switch(requestcode) { case 1: dbaddmovie(data.getstringextra("title"), data.getstringextra("year")); break; case 2: testid = data.getlongextra("rowid", -1); dmovie(testid); break; } } } //adds item movie list public void dbaddmovie(string mt, string my) { moviedatabase.open(); moviedatabase.insertmovie(mt, my); toast.maketext(this, "movie: " + mt + " added database", toast.length_short).show(); } //deletes entry database public void dmovie(long rowid) { //toast.maketext(this, "deleting: " + rowid, toast.length_short).show(); moviedatabase.deletemovie(rowid); moviedatabase.getallmovies(); } //displays database list public void displaylistview() { addbutton = (button) findviewbyid(r.id.add); addbutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { startactivityforresult(addm, 1); } }); cursor = moviedatabase.getallmovies(); //columns use string[] columns = new string[] { moviedatabase.key_title, }; //xml data bind data int[] = new int[] { r.id.column2, }; //adapter display database list dataadapter = new simplecursoradapter(this, r.layout.complexrow, cursor, columns, to, 0); //gets list view resource lv = (listview) findviewbyid(r.id.movielist); //sets list view use adapter lv.setadapter(dataadapter); //handles list click events lv.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view v, int position, long id) { cursor cursor = (cursor) parent.getitematposition(position); bundle mdet = new bundle(); mdet.putstring("title", cursor.getstring(cursor.getcolumnindex(moviedatabase.key_title))); mdet.putstring("year", cursor.getstring(cursor.getcolumnindex(moviedatabase.key_year))); mdet.putint("rid", position); viewm.putextras(mdet); startactivityforresult(viewm, 2); } }); //dataadapter.notifydatasetchanged(); } public void onresume() { super.onresume(); displaylistview(); } }
and coresponding dbadapter class
public class dbadapter { public static final string key_rowid = "_id"; public static final string key_title = "title"; public static final string key_year = "year"; private static final string tag = "dbadapter"; private static final string database_name = "movielistdb"; private static final string database_table = "moviestable"; private static final int database_version = 1; private static final string database_create = "create table moviestable (_id integer primary key autoincrement, " + "title text not null, year not null);"; private final context context; private databasehelper dbhelper; private sqlitedatabase db; public dbadapter(context ctx) { this.context = ctx; dbhelper = new databasehelper(context); } private static class databasehelper extends sqliteopenhelper { databasehelper(context context) { super(context, database_name, null, database_version); } @override public void oncreate(sqlitedatabase db) { try{ db.execsql(database_create); } catch (sqlexception e) { e.printstacktrace(); } } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { log.w(tag, "upgrading database version " + oldversion + " " + newversion + "which destroy old data"); db.execsql("drop table if exists moviestable"); oncreate(db); } } public dbadapter open() throws sqlexception { db = dbhelper.getwritabledatabase(); return this; } public void close() { dbhelper.close(); } public long insertmovie(string title, string year) { contentvalues initialvalues = new contentvalues(); initialvalues.put(key_title, title); initialvalues.put(key_year, year); return db.insert(database_table, null, initialvalues); } public boolean deletemovie(long rowid) { return db.delete(database_table, key_rowid + "='" + rowid+"'", null ) >-1; } public cursor getallmovies() { return db.query(database_table, new string[] {key_rowid, key_title, key_year}, null, null, null, null, null); } public cursor getmovie(long rowid) throws sqlexception { cursor mcursor = db.query(true, database_table, new string[] {key_rowid, key_title, key_year}, key_rowid + "=" + rowid, null, null, null, null, null); if(mcursor != null) { mcursor.movetofirst(); } return mcursor; } public boolean updatecontact(long rowid, string title, string year) { contentvalues args = new contentvalues(); args.put(key_title, title); args.put(key_year, year); return db.update(database_table, args, key_rowid + "=" + rowid, null) > 0; } public void deleteallmovies() { int donedelete = 0; donedelete = db.delete(database_table, null, null); } }
you're using
position
returned listview row id in database. won't match autoincremented"_id"
in database.position
position in list is.you might want think using
moviedatabase.key_rowid
key intents. right see mix of"rowid"
,"rid"
,"_id"
, ,key_rowid
. simplify thing use same key everywhere when referring same thing.it looks continuously add bundles
viewm
intent. true? if that's not intent, should either create new intent each click, or remove previous bundles first.
Comments
Post a Comment