java - Updating a TextView in a ViewPager Fragment -


in android app, i'm having trouble setting textview located in fragment parent activity. using viewpager follows:

in main activity's oncreate():

list<fragment> fragments = new vector<fragment>(); fragments.add(fragment.instantiate(this, mainpageprofilefragment.class.getname())); fragments.add(fragment.instantiate(this, mainpageotherfragment.class.getname())); mainpageradapter = new viewpageradapter(this.getsupportfragmentmanager(), fragments); viewpager pager = (viewpager)findviewbyid(r.id.viewpager); pager.setadapter(mainpageradapter); 

with viewpageradapter:

public class viewpageradapter extends fragmentpageradapter{     private list<fragment> fragments;     public viewpageradapter(fragmentmanager fm, list<fragment> fragments) {         super (fm);         this.fragments = fragments;     }     @override     public fragment getitem(int arg0) {         return this.fragments.get(arg0);     } } 

and mainpageprofilefragment below. fragment provides interface update textview @ runtime (after view created).

public class mainpageprofilefragment extends fragment     implements view.onclicklistener {     private textview profileimagelabeltextview;     @override     public view oncreateview(layoutinflater inflater, viewgroup container,                          bundle savedinstancestate) {         if(container == null) {             return null;         }         scrollview view = (scrollview)inflater.inflate(r.layout.fragment_main_page_profile, container, false);         labeltextview = (textview) view.findviewbyid(r.id.profilelabel);         return view;     }     public void setlabeltext(string text) {         labeltextview.settext(text);     } } 

the setlabeltextview() i'd call main activity's onresume() handler, haven't yet been able handle on fragment allow me access method. i'm going bit in circles have found different approaches accessing views in fragments seemed plausible haven't succeeded yet. how call setlabeltext() method activity?

@override protected void onresume() {     super.onresume();     // set labeltextview here } 

the straitforward way lookup viewpager, take viewpageradapter , try fragment. if fragment there, can call setlabeltext() method. this.

@override protected void onresume() {     super.onresume();      viewpager pager = (viewpager)findviewbyid(r.id.viewpager);     viewpageradapter adapter = (viewpageradapter) pager.getadapter();     fragment fragment = adapter.getitem(0);     if (fragment instanceof mainpageprofilefragment) {         // if here, fragment not null , has correct type         ((mainpageprofilefragment)fragment).setlabeltext("my text");     } } 

to add getitem() method adapter, should extend fragmentpageradapter , track list of fragments own. this

public class pageradapter extends fragmentpageradapter {      private list<fragment> mfragments = new arraylist<fragment>();      @override     public fragment getitem(int position) {         return mfragments.get(position);     }      @override     public int getcount() {         return mfragments.size();     }      public void addfragment(fragment fragment) {         mfragments.add(fragment);         notifydatasetchanged();     } } 

but more elegant , flexible solution use event bus activity fragment communication. there event buses implementation out there. can try tinybus or eventbus.


Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -