android - Update Listview after deleting database item -
ive been trying listview update after removing item. here's have far:
mylist.setonitemclicklistener(new adapterview.onitemclicklistener() { string str = null; public void onitemclick(adapterview<?> arg0, final view view, int arg2, long arg3) { //textview txtview = (textview)view.findviewbyid(r.id.txtview); final string item = ((textview) view.findviewbyid(r.id.txtview)).gettext().tostring(); str = item; final long arr = arg3; final string arg22 = longtostring(arg3); //creating alert dialog alertdialog.builder builder = new alertdialog.builder(home.this); builder.setmessage("are sure want delete hike " + str + " ?"); builder.setcancelable(false); builder.setpositivebutton("yes", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int which) { sqlitedatabase db1=openorcreatedatabase("hikemanager", mode_private, null); databasehandler db = new databasehandler(home.this); string table = "hikes"; cursor c = db1.rawquery("select id "+ table + " name='"+item+"'", null); int dhike = c.getcolumnindex("name") + 1; try { hike hike = db.gethike(arr + 1); db.deletehike(hike); log.d("dlt", "deleted hike @ index " + arr); //db.updatelist(adapter, mylist, listitems); adapter.remove(arg22); adapter.notifydatasetchanged(); //updatedata(); db.close(); } catch (cursorindexoutofboundsexception e) { log.d("dlt", "failed delete: " + e.getmessage()); db.close(); } //db.updatelist(adapter, mylist, listitems); } }); builder.setnegativebutton("no", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int which) { // todo auto-generated method stub dialog.cancel(); } }); alertdialog alert = builder.create(); alert.show(); } });
i have quite bit of unused code in there, have tried few different methods work, have failed far. here updatedata:
private void updatedata() { // of notes database , create item list databasehandler db = new databasehandler(this); final arrayadapter<string> adapter = new arrayadapter<string>(this, r.layout.list_item, r.id.txtview, listitems); final listview mylist = (listview) findviewbyid(r.id.cardlistview); int num = db.gethikescount(); (int i=1; i<num+1; ++i){ hike name = db.gethike(i); string nam = name.getname(); listitems.add(nam); } mylist.setadapter(adapter); db.close(); }
the updatedata have unintended consequences when use update view after adding item non-empty list, works now. item deleted, since can close app , reload , item gone. cant seem update me.
just use
adapter.notifydatasetchanged();
Comments
Post a Comment