android - ListView does not show changes until focus changes after notifyDataSetChanged -
i have alertdialog
listview
set multiple selection on it. has button
on it.
the button
open alertdialog
if ok'ed remove selected items data set of listview
, , tell adapter of list view dataset has changed notifydatasetchanged()
method.
this works fine except 1 thing. listview
not update it's content until interact something. updates correct data.
this not big problem, listview
appear correct @ once, , not after focus has changed.
code:
button remove = (button) view.findviewbyid(r.id.btn_remove_questions_edit_rack); final context con = this; remove.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { builder warnbuild = new builder(con); warnbuild.setmessage(r.string.question_deletion_warning); warnbuild.setpositivebutton(r.string.ok, new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { sparsebooleanarray checked = list.getcheckeditempositions(); (string s : keys) { int = keys.indexof(s); if (checked.get(i)) { toremove.add(map.get(s)); map.remove(s); } } keys.clear(); keys.addall(map.keyset()); ((arrayadapter) list.getadapter()).notifydatasetchanged(); list.clearchoices(); //this makes sure selection cleared, if isn't, of other items (those has index of selected items) selected when view refreshes. dialog.dismiss(); } }); //negative button here, not relevant. } });
where map
, keys
are:
final hashmap<string, qualityquestion> map = new hashmap<>(); //i add items map final arraylist<string> keys = new arraylist<>(map.keyset());
and toremove
store items removed actual object on when ok button on original alertdialog
pressed.
this how populate listview
in first place:
final listview list = (listview) view.findviewbyid(r.id.list_questions_edit_rack); list.setadapter( new arrayadapter<string>(this, android.r.layout.simple_list_item_activated_1, keys));
i have tried things list.invalidateviews()
, list.invalidate
, other things found in questions similar mine here on so. none of made difference. suspect problem different theirs since items updated, takes change of focus on original alertdialog
change visible.
how can make listview
show changes in it's data source imidiatly insted of after focus change?
by calling
((arrayadapter) list.getadapter()).notifydatasetchanged();
you fresh adapter not identical anonymous adapter used populate list in first instance.
see documentation listview.getadapter()
returns adapter in use in listview. returned adapter might not same adapter passed setadapter(listadapter) might wrapperlistadapter.
from point of view of fresh adapter, data set hasn't changed because changes happened way before instantiated.
to solve problem, make list , list adapter members of activity class (or scope want keep them alive):
private arraylist<string> keys; private arrayadapter myadapter; private listview list;
then in "oncreate()"
keys = ...; // initialization of arraylist needed data myadapter = new arrayadapter<string>(this, android.r.layout.simple_list_item_activated_1, keys); list = (listview) view.findviewbyid(r.id.list_questions_edit_rack); list.setadapter(myadapter);
this way, in "onclicklistener" can notify "myadapter":
keys.addall(map.keyset()); myadapter.notifydatasetchanged();
hope helps :)
Comments
Post a Comment