android - How to pass ArrayAdapter<String> from Activity to Fragment -


i have app 1 activity , 3 fragments.

in activity have adapter, store log messages -

mainactivity.java (keeps adapter strings):

private arrayadapter<string> mloglistadapter;  public void oncreate(bundle savedinstancestate) {      .....     mloglistadapter = new arrayadapter<string>(this,                       android.r.layout.simple_list_item_1,                       android.r.id.text1);      if (savedinstancestate == null) {         mainfragment fragment = new mainfragment();         bundle bundle = new bundle();         bundle.putserializable("log", (serializable) mloglistadapter);         //bundle.putparcellable("log", (parcellable) mloglistadapter);         getfragmentmanager().begintransaction()             .replace(r.id.root, fragment, "main")             .commit();     } } 

and use adapter in first fragment -

mainfragment.java (should display list log strings):

private listview mloglist;  public view oncreateview(layoutinflater inflater,          viewgroup container,         bundle savedinstancestate) {     view view = inflater.inflate(r.layout.activity_main, container, false);     .........            mloglist = (listview) view.findviewbyid(r.id.loglist);     mloglist.setchoicemode(listview.choice_mode_none);      // not work     listadapter adapter =          (listadapter) savedinstancestate.getserializable("log");     mloglist.setadapter(adapter);      return view; } 

unfortunately, not work (app crashes).

i have tried adding public method fragment , calling in activity - mloglist null , npe (as mloglist created later - not in constructor, in oncreateview method):

mainactivity.java:

    if (savedinstancestate == null) {         mainfragment fragment = new mainfragment();         fragment.setadapter(mloglistadapter);         getfragmentmanager().begintransaction()             .replace(r.id.root, fragment, "main")             .commit();     } 

mainfragment.java:

    public void setadapter(listadapter adapter) {         mloglist.setadapter(adapter); // gives npe     } 

please advise how pass adapter fragment.

update:

i've tried exception lover's suggestion (thanks +1), error:

the method putparcelablearraylist(string, arraylist) in type bundle not applicable arguments (string, arrayadapter)

and not, sure of quickfix suggestions should take:

eclipse screenshot

also wonder, why can't savedinstancestate used - need create new bundle object when passing data activity fragment?

mainactivity.java (keeps adapter strings) change code

private arrayadapter<string> mloglistadapter;  public void oncreate(bundle savedinstancestate) {      .....     mloglistadapter = new arrayadapter<string>(this,                       android.r.layout.simple_list_item_1,                       android.r.id.text1);      if (savedinstancestate == null) {         mainfragment fragment = new mainfragment();         bundle bundle = new bundle();         bundle.putparcelablearraylist("arraylist", data);         fragment.setarguments(bundle);         getfragmentmanager().begintransaction()             .replace(r.id.root, fragment, "main")             .commit();     } } 

and change

public view oncreateview(layoutinflater inflater,          viewgroup container,         bundle savedinstancestate) {     view view = inflater.inflate(r.layout.activity_main, container, false);     .........             mloglist = (listview) view.findviewbyid(r.id.loglist); bundle extras = getarguments(); if (extras != null) {     data = extras.getparcelablearraylist("arraylist");     mloglist.setadapter(new myadapter(getactivity(), data)); }     mloglist.setchoicemode(listview.choice_mode_none);       return view; }  

Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

node.js - How to abort query on demand using Neo4j drivers -