android - Unable to refresh listView from other class -
in main fragment, have listview called noteslistview
. noteadapter
populates noteslistview
. when user long clicks on 1 of noteslistview
's elements, dialog shows , asks if user wants remove item. if agrees, item removed database. if not - life goes on.
the issue dialog other class (other fragment). class, pass database
object , noteadapter
object well, remove item database , notify noteadapter
data has changed. sounds enough, doesn't work, , have absolutely no idea why. give please , me out.
this method in mainfragment, handles mentioned listview:
public void handlenotes(final listview noteslistview) {
if (database.getnotecount() != 0) { noteslistview.setadapter(noteadapter); noteslistview.setonitemlongclicklistener(new adapterview.onitemlongclicklistener() { @override public boolean onitemlongclick(adapterview<?> adapterview, view view, int i, long l) { textview textviewid = (textview) view.findviewbyid(r.id.textviewid); deletenotefragment newfragment = new deletenotefragment(database, noteadapter, integer.parseint(textviewid.gettext().tostring())); newfragment.show(getactivity().getsupportfragmentmanager(), "deleteconfirmation"); return false; } }); }
}
as can see, deletenotefragment being created , shown. lets @ deletenotefragment itself:
public class deletenotefragment extends dialogfragment { private database database; private noteadapter noteadapter; private int i; public deletenotefragment(database database, noteadapter noteadapter, int i) { this.database = database; this.noteadapter = noteadapter; this.i = i; } @nonnull @override public dialog oncreatedialog(bundle savedinstancestate) { // use builder class convenient dialog construction alertdialog.builder builder = new alertdialog.builder(getactivity()); builder.setmessage(r.string.dialog_delete_note) .setpositivebutton(r.string.dialog_delete_confirm, new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int id) { database.removenote(i); noteadapter.notifydatasetchanged(); toast.maketext(getactivity(), "note deleted successfully!", toast.length_long).show(); } }) .setnegativebutton(r.string.dialog_delete_denny, new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int id) { // user cancelled dialog } }); // create alertdialog object , return return builder.create(); } }
maybe can spot making mistake, or got tips how solve issue?
you deleting data in database not in adapter:
database.removenote(i); noteadapter.notifydatasetchanged();
creates method in adapter list have in adapter. like:
database.removenote(i); noteadapter.getlistofitems().remove(i); noteadapter.notifydatasetchanged();
Comments
Post a Comment