android - Get id of TextView in Fragment from FragmentActivity in ViewPager -


i'm working viewpager 3 fragments , want change text of textview in third page.

in page have buttonthat when pressed, go sd images select one. when done, returns page , want update textview path of image. problem when try access textviewfrom fragmentactivityit null.

here code

sherlockfragmentactivity:

public class tabsfacturasactivity extends sherlockfragmentactivity {      protected myapplication myapplication;     private static final int file_select_code = 0;      private myadapter madapter;     private viewpager mpager;     private pageindicator mindicator;     private textview textviewimg;      private int lecturas = 0;     private simpledateformat dateformat = new simpledateformat("dd/mm/yyyy");      private boolean adjunto = false;     private string filepath;     private boolean eslecturaat = false;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.fragment_pager);          // application instance         myapplication = (myapplication)getapplication();          //need view         textviewimg = (textview) findviewbyid(r.id.textviewurlimglectura);          //creamos la lista         linkedlist<string> direcciones = new linkedlist<string>();         arraylist<tuplacupsws> dirs = myapplication.getusuarioactual().getcups();         for(int dir = 0; dir < myapplication.getusuarioactual().getcups().size(); dir++) {             direcciones.add(new string(dirs.get(dir).getdireccion()));         }          int tab = getintent().getintextra("tab", 0);          madapter = new myadapter(getsupportfragmentmanager());          mpager = (viewpager)findviewbyid(r.id.pager);         mpager.setadapter(madapter);         mpager.setcurrentitem(tab);          mindicator = (titlepageindicator)findviewbyid(r.id.indicator);         mindicator.setviewpager(mpager);          getsupportactionbar().setdisplayshowtitleenabled(false);         getsupportactionbar().seticon(r.drawable.logo_factorenergia_peque);          /** create array adapter populate dropdownlist */         arrayadapter<string> adapter = new arrayadapter<string>(                 getbasecontext(), android.r.layout.simple_spinner_dropdown_item, direcciones);          /** enabling dropdown list navigation action bar */         getsupportactionbar().setnavigationmode(actionbar.navigation_mode_list);          /** defining navigation listener */         onnavigationlistener navigationlistener = new onnavigationlistener() {              @override             public boolean onnavigationitemselected(int itemposition, long itemid) {                 return false;             }         };          /** setting dropdown items , item navigation listener actionbar */         getsupportactionbar().setlistnavigationcallbacks(adapter,                  (com.actionbarsherlock.app.actionbar.onnavigationlistener)                  navigationlistener);     }      @override     protected void onactivityresult(int requestcode, int resultcode, intent data) {         switch (requestcode) {             case file_select_code:             if (resultcode == result_ok) {                 adjunto = true;                 // uri of selected file                  uri uri = data.getdata();                 // path                 string path = "";                 try {                     path = myutility.getpath(this, uri);                 } catch (urisyntaxexception e) {                     myapplication.throwexception(this);                     e.printstacktrace();                 }                 string imgname = path.split("/")[path.split("/").length-1];                 textviewimg.settext(imgname); //here textviewimg null                 filepath = path;             }             break;         }         super.onactivityresult(requestcode, resultcode, data);     }      //method executed when button pressed     public void examinar(view view) {         mostrarfilechooser();            }      private void mostrarfilechooser() {              intent intent = new intent(intent.action_pick);         intent.settype("image/*");          try {             startactivityforresult(intent, file_select_code);         } catch (android.content.activitynotfoundexception ex) {         }     }      private static class myadapter extends fragmentpageradapter {          private string[] titles = { "ver facturas", "ver consumo", "introducir lectura" };          public myadapter(fragmentmanager fragmentmanager) {             super(fragmentmanager);         }          @override         public charsequence getpagetitle(int position) {             return titles[position];         }          @override         public fragment getitem(int position) {             switch (position) {             case 0: // fragment # 0                 return new facturasactivity();             case 1: // fragment # 1                 return new consumoactivity();             case 2:// fragment # 2                 return new lecturaactivity();             }             //return new myfragment();             return null;         }          @override         public int getcount() {             return titles.length;         }     }  } 

fragment_pager.xml

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="fill_parent"     android:layout_height="fill_parent"     android:orientation="vertical"     tools:context=".tabsfacturasactivity" >      <com.viewpagerindicator.titlepageindicator         android:id="@+id/indicator"         android:padding="10dip"         android:layout_height="wrap_content"         android:layout_width="fill_parent" />      <android.support.v4.view.viewpager         android:id="@+id/pager"         android:layout_width="fill_parent"         android:layout_height="0dp"         android:layout_weight="1"         android:background="#1184a4e8" />  </linearlayout> 

you cant access text view fragment activity because located on fragment , fragment activity layout don't have text view id. have access text view third fragment used in layout. access object fragment activity.

in fragment this

textview mtextview; mtextview = (textview)getview().findviewbyid(r.id.your_text_view); 

create function this

public void changetext(string mtext) { mtextview.settext(mtext); } 

in activity result

//do not create new object each time set text. use same fragment object use view pager.         your_fragment mfragment;          protected void onactivityresult(int requestcode, int resultcode, intent data) {                 switch (requestcode) {                     case file_select_code:                     if (resultcode == result_ok) {      //set text here                 mfragment.changetext(imgname);                 } 

Comments

Popular posts from this blog

Unable to remove the www from url on https using .htaccess -