Wednesday, 15 July 2015

android - Replace ListFragment -



android - Replace ListFragment -

i using listfragment displaying list database in activity. have included search function. unfortunately "old" listfragments seem remain in background , listfragments containing result of query displayed on top of it. how can avoid, old listfragments displayed?

my fragmentactivity:

private button buttonsearch; private textview searchstring; public static string search = null; static ishoppinglist shoppinglistmanager; static iaktionen aktionenmanager; private aktionenlistlistfragment listfragment; @override public void oncreate(bundle savedinstancestate) { log.d(tag, "listfragmentactivity created"); super.oncreate(savedinstancestate); setcontentview(r.layout.articlelist); shoppinglistmanager = new shoppinglist(this); aktionenmanager = new aktionen(this); buttonsearch = (button) findviewbyid(r.id.search_button); buttonsearch.setonclicklistener(searchlistaktionen); //show entries on start listfragment = new aktionenlistlistfragment(); getsupportfragmentmanager().begintransaction().replace(r.id.fragment_articlelist, listfragment).commit(); } onclicklistener searchlistaktionen = new onclicklistener() { public void onclick(view v) { try{ searchstring = (textview) findviewbyid(r.id.input_search_bezeichnung); search = searchstring.gettext().tostring().trim(); log.d(tag, "search button clicked "+search); if(search.trim().length()==0){ search=null; } //show entries on start listfragment = new aktionenlistlistfragment(); getsupportfragmentmanager().begintransaction().replace(r.id.fragment_articlelist, listfragment).commit(); }catch(exception ex){ ex.printstacktrace(); } } };

thanks in advance,

update:

thank answers. tried implement them, main problem seems nowthat oncreate , onactivitycreated method in listfragment called twice (as can see in log messages).

my new code:

@override public void oncreate(bundle savedinstancestate) { log.d(tag, "listfragmentactivity created"); super.oncreate(savedinstancestate); //force commit getsupportfragmentmanager().executependingtransactions(); if(getsupportfragmentmanager().findfragmentbytag(tag) == null) { setcontentview(r.layout.articlelist); shoppinglistmanager = new shoppinglist(this); aktionenmanager = new aktionen(this); buttonsearch = (button) findviewbyid(r.id.search_button); buttonsearch.setonclicklistener(searchlistaktionen); listfragment = new aktionenlistlistfragment(); getsupportfragmentmanager().begintransaction().replace(r.id.fragment_articlelist, listfragment,tag).commit(); }else{ log.d(tag, "listfragment exists"); } }

i tried set unique tag listfragment solution not work.

i guess 1 of listfragments displayed in background , other updated.

so first need stop making new listfragments everytime list refreshed , have public method in listfragment activity can phone call restart loader proper parameters. then:

in onloadfinished(),

you should create new adapter list want replace

myadapter = new aktionenlistcustomcursoradapter(getactivity(), mycursor);

and call:

this.getlistview().setadapter(myadapter);

so:

public void onloadfinished(loader<cursor> madapter, cursor mycursor) { if(mycursor!=null){ //getting info database log.d(tag, "search string "+aktionenlistfragmentactivity.search); if(aktionenlistfragmentactivity.search==null){ mycursor = aktionenlistfragmentactivity.aktionenmanager.fetchallarticles(); }else{ mycursor = aktionenlistfragmentactivity.aktionenmanager.fetchitemsbybezeichnung(aktionenlistfragmentactivity.search); } myadapter = new aktionenlistcustomcursoradapter(getactivity(), mycursor); this.getlistview().setadapter(myadapter); } }

hopefully solved question understood it. if have questions please leave in comment below , expand answer. if worked please take answer. :d

android fragment

No comments:

Post a Comment