java - Set text inside fragment not working after revisiting fragment -


i developing bus time table android app. have 3 fragments. inside first fragment have 2 radio buttons i.e. from malegaon & to malegaon. (malegaon name of place).

if select from malegaon radio button setting text sourceedittext malegaon. , if select to malegaon radio button setting text destinationedittext malegaon.

this condition working fine, when visit fragment first time, if revisit fragment from malegaon radio button selected, sourceedittext blank , destinationedittext has text malegaon.

here snapshot , code first fragment.

from malegaon radio button selected

after selecting malegaon radio button.

enter image description here

i changing visibility of layout. (source edittext,destination edittext,search button 1 layout)

oldstandfragment.java

public class oldstandfragment extends fragment {  public static oldstandfragment fragment ; private static final string arg_positions = "position"; private int positions; private view myfragmentviewold; private linearlayout fromoldmalegoanview, tooldmalegoanview; button selectroutebutton; public static final string required_dest = "please enter destination"; public static final string required_source = "please enter source"; string language = ""; dbhelper helper; private string sourceid = "", destinationid = ""; private arraylist<route> myarraylist; private routeadapter routeadapter; private listview routelistview; private edittext sourceedittextfrommalegoanold; private edittext destinationedittextfrommalegoanold; private imagebutton searchfrommalegoanbuttonold; private edittext sourceedittexttomalegoanold; private edittext destinationedittexttomalegoanold; private imagebutton searchtomalegoanbuttonold; private radiobutton fromoldmalegoanradiobutton, tooldmalegoanradiobutton;  public oldstandfragment() {     // required empty public constructor }  @override public void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     positions = getarguments().getint(arg_positions);  }   @override public view oncreateview(layoutinflater inflater, viewgroup container,                          bundle savedinstancestate) {      myfragmentviewold = inflater.inflate(r.layout.fragment_old_stand, container, false);          selectroutebutton = (button) myfragmentviewold.findviewbyid(r.id.selectroutebutton);          fromoldmalegoanradiobutton = (radiobutton) myfragmentviewold.findviewbyid(r.id.fromoldmalegoanradiobutton);         tooldmalegoanradiobutton = (radiobutton) myfragmentviewold.findviewbyid(r.id.tooldmalegoanradiobutton);          fromoldmalegoanview = (linearlayout) myfragmentviewold.findviewbyid(r.id.fromoldmalegoanview);         tooldmalegoanview = (linearlayout) myfragmentviewold.findviewbyid(r.id.tooldmalegoanview);          sourceedittextfrommalegoanold = (edittext) fromoldmalegoanview.findviewbyid(r.id.sourceedittext);         destinationedittextfrommalegoanold = (edittext) fromoldmalegoanview.findviewbyid(r.id.destinationedittext);         searchfrommalegoanbuttonold = (imagebutton) fromoldmalegoanview.findviewbyid(r.id.searchresultbutton);          sourceedittexttomalegoanold = (edittext) tooldmalegoanview.findviewbyid(r.id.sourceedittext);         destinationedittexttomalegoanold = (edittext) tooldmalegoanview.findviewbyid(r.id.destinationedittext);         searchtomalegoanbuttonold = (imagebutton) tooldmalegoanview.findviewbyid(r.id.searchresultbutton);          sharedpreferences prefs = getcontext().getsharedpreferences("myprefsfile", context.mode_private);         int = prefs.getint("langvalue", 0);         if (a == 0) {             language = "english";         } else {             language = "मराठी";         }          helper = new dbhelper(getcontext());         fromoldmalegoanradiobutton.setchecked(true);         tooldmalegoanradiobutton.setchecked(false);         fromoldmalegoanview.setvisibility(view.visible);         tooldmalegoanview.setvisibility(view.gone);          string stopvalue = helper.getstopname("1", language);         sourceedittextfrommalegoanold.settext(stopvalue);          fromoldmalegoanradiobutton.setoncheckedchangelistener(new compoundbutton.oncheckedchangelistener() {             @override             public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) {                 if (fromoldmalegoanradiobutton.ischecked()) {                      tooldmalegoanradiobutton.setchecked(false);                     fromoldmalegoanview.setvisibility(view.visible);                     tooldmalegoanview.setvisibility(view.gone);                      helper = new dbhelper(getcontext());                     string stopvalue1 = helper.getstopname("1", language);                     sourceedittextfrommalegoanold.settext(stopvalue1);                     destinationedittextfrommalegoanold.settext("");                 }             }         });          tooldmalegoanradiobutton.setoncheckedchangelistener(new compoundbutton.oncheckedchangelistener() {             @override             public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) {                 if (tooldmalegoanradiobutton.ischecked()) {                      fromoldmalegoanradiobutton.setchecked(false);                     fromoldmalegoanview.setvisibility(view.gone);                     tooldmalegoanview.setvisibility(view.visible);                      helper = new dbhelper(getcontext());                     string stopvalue2 = helper.getstopname("1", language);                     destinationedittexttomalegoanold.settext(stopvalue2);                     sourceedittexttomalegoanold.settext("");                 }             }         });          searchfrommalegoanbuttonold.setonclicklistener(new view.onclicklistener() {             @override             public void onclick(view v) {                //search result code.             }         });          searchtomalegoanbuttonold.setonclicklistener(new view.onclicklistener() {             @override             public void onclick(view v) {                //search result code.             }         });     return myfragmentviewold; }  public static oldstandfragment newinstance(int position) {     if(fragment == null) {         fragment = new oldstandfragment();     }      bundle bundle = new bundle();     bundle.putint(arg_positions, position);     fragment.setarguments(bundle);     return fragment;     } } 

fragment_old_stand.xml

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="#000000" android:scrollbars="vertical" tools:context="com.ashishkudale.malegoanagar.fragments.oldstandfragment">   <scrollview     android:layout_width="match_parent"     android:layout_height="match_parent">      <linearlayout         android:orientation="vertical"         android:layout_width="match_parent"         android:layout_height="match_parent">          <linearlayout             android:orientation="horizontal"             android:layout_width="match_parent"             android:layout_height="wrap_content">              <textview                 android:layout_width="match_parent"                 android:layout_height="wrap_content"                 android:text="select direction or route"                 android:gravity="center"                 android:textcolor="#ffffff"                 android:id="@+id/note"                 android:textstyle="bold"                 android:textappearance="?android:attr/textappearancemedium"                 android:layout_margin="15dp" />         </linearlayout>           <linearlayout             android:orientation="horizontal"             android:layout_width="match_parent"             android:layout_height="wrap_content">              <radiogroup                 android:id="@+id/rg_containerold"                 android:layout_width="match_parent"                 android:layout_height="match_parent"                 android:orientation="vertical"                 android:layout_margintop="15dp">                  <radiobutton                     android:layout_width="match_parent"                     android:layout_height="wrap_content"                     android:text="from malegaon"                     android:id="@+id/fromoldmalegoanradiobutton"                     android:layout_marginleft="5dp"                     android:checked="false" />                  <linearlayout                     android:orientation="vertical"                     android:layout_width="match_parent"                     android:layout_height="wrap_content"                     android:layout_margintop="10dp">                      <include                         android:id="@+id/fromoldmalegoanview"                         android:layout_width="match_parent"                         android:layout_height="wrap_content"                         layout="@layout/source_destination"                         android:layout_margin="5dp" />                  </linearlayout>                  <radiobutton                     android:layout_width="match_parent"                     android:layout_height="wrap_content"                     android:text="to malegaon"                     android:id="@+id/tooldmalegoanradiobutton"                     android:layout_marginleft="5dp" />                  <linearlayout                     android:orientation="vertical"                     android:layout_width="match_parent"                     android:layout_height="wrap_content"                     android:layout_margintop="10dp">                      <include                         android:id="@+id/tooldmalegoanview"                         android:layout_width="match_parent"                         android:layout_height="wrap_content"                         layout="@layout/source_destination"                         android:layout_margin="5dp" />                  </linearlayout>              </radiogroup>         </linearlayout>          <button             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:text="  select route  "             android:id="@+id/selectroutebutton"             android:background="@drawable/button_effect"             android:layout_gravity="center_horizontal"             android:layout_margin="5dp" />      </linearlayout>  </scrollview> 

this adapter call fragment.

mypageradapter

public class mypageradapter extends fragmentpageradapter {      private final string[] titles = {"old stand","new stand", "fare"};      public mypageradapter(fragmentmanager fm) {         super(fm);     }      @override     public charsequence getpagetitle(int position) {         return titles[position];     }      @override     public int getcount() {         return titles.length;     }      @override     public android.support.v4.app.fragment getitem(int position) {         android.support.v4.app.fragment fragment = null;         if(position ==0) {             fragment = oldstandfragment.newinstance(position);         }else if(position ==1 ){             fragment = newstandfragment.newinstance(position);         } else if (position == 2) {             fragment = mapfragment.newinstance(position);         }         return  fragment;     } } 

after revisiting oldstandfragment this.

enter image description here

i checked adding logs everywhere possible. , found after revisiting oldstandfragment, tooldmalegoanradiobutton.setonclicklistner() method getting called.

now want refresh fragment when re-visit or other way solve issue.

you have use sharedpreferences save state of checkbox, try code

    public class stackone extends appcompatactivity {      sharedpreferences prefs;     private radiobutton rbutton1, rbutton2;     private radiogroup rg_containerold;      @override     public void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.stack_one);          prefs = preferencemanager.getdefaultsharedpreferences(stackone.this);          rbutton1 = (radiobutton) findviewbyid(r.id.fromoldmalegoanradiobutton);         rbutton2 = (radiobutton) findviewbyid(r.id.tooldmalegoanradiobutton);         rg_containerold = (radiogroup) findviewbyid(r.id.rg_containerold);          getselectedradiobutton();          int k = prefs.getint("rb1", 0);         if (k == 1) {             rbutton1.setchecked(true);             sourceedittextfrommalegoanold.settext("");         } else if (k == 2) {             rbutton2.setchecked(true);             sourceedittexttomalegoanold.settext("");         }     }      private void getselectedradiobutton() {          rg_containerold.setoncheckedchangelistener(new radiogroup.oncheckedchangelistener() {             @override             public void oncheckedchanged(radiogroup group, int checkedid) {                  switch (group.getcheckedradiobuttonid()) {                     case r.id.fromoldmalegoanradiobutton:                         prefs.edit().putint("rb1", 1).commit();                         break;                     case r.id.tooldmalegoanradiobutton:                         prefs.edit().putint("rb1", 2).commit();                         break;                 }             }         });     } } 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -