java - Fragment content does not show up until I switch it and select it again -
i've got problem android app: fragment content doesn't show until switch , select again. should load @ fragment start list of cards.
here got oncreate method of fragment loads everything:
@override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); bundle = this.getarguments(); debts = bundle.getparcelablearraylist("list"); adapter = new cardadapter(getactivity(), android.r.color.holo_blue_dark); (int i=0; i<debts.size();++i) { debt d = debts.get(i); card card = new card(d.getseconduser(),d.getsubject()); adapter.add(card); } }
and here onactivitycreated method set adapter list:
@override public void onactivitycreated(bundle savedinstancestate) { super.onactivitycreated(savedinstancestate); cardlistview list = (cardlistview) getview().findviewbyid(r.id.newcard); list.setcardtheme(cardtheme.light); list.setadapter(adapter); }
it works if use arraylist of cards loaded, not way. tried everything... thank much.
update: tried solutions gave me, keeps happening, code now:
@override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); bundle = this.getarguments(); debts = bundle.getparcelablearraylist("list"); adapter = new cardadapter(getactivity(), android.r.color.holo_blue_dark); (int i=0; i<debts.size();++i) { debt d = debts.get(i); card card = new card(d.getseconduser(),d.getsubject()); adapter.add(card); } } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.fragment_debts, container, false); cardlistview list = (cardlistview) rootview.findviewbyid(r.id.newcard); list.setcardtheme(cardtheme.light); list.setadapter(adapter); return rootview; }
you should initialize cardlistview inside oncreateview() method. oncreateview() gets called before onactivitycreated() method. oncreateview() should point when view of fragment initialized.
@override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { //use layout file instead of r.layout.fragment_main view rootview = inflater.inflate(r.layout.fragment_main, container, false); cardlistview list = rootview.findviewbyid(r.id.newcard); list.setcardtheme(cardtheme.light); list.setadapter(adapter); }
please take @ the lifecycle of fragments.
Comments
Post a Comment