Billy Batson Billy Batson - 4 months ago 8
Android Question

Where do I define textViews within a fragment to change its content multiple time?

so I have this fragment which includes multiple TextView that gets changed when user clicks on a "next" button or "previous" button.

within the onCreateView method i have:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View inflatedView = inflater.inflate(R.layout.fragment_time_table, container, false);


update(inflatedView,worker.current.getTarih(),worker.current.getHicri(),worker.current.getGün(),worker.current.getImsak(),worker.current.getSabah(),worker.current.getGüneş(),worker.current.getÖğle(),worker.current.getIkindi(),worker.current.getAkşam(),worker.current.getYatsı());


// Inflate the layout for this fragment
return inflatedView;


}


on the update method I have

public void update(View dimension,String tarih, String hicri, String gun, String imsak, String sabah, String gunes, String ogle, String ikindi, String aksam, String yatsi){

TextView gunView = (TextView) dimension.findViewById(R.id.gun);
TextView imsakView = (TextView) dimension.findViewById(R.id.imsak);
TextView sabahView = (TextView) dimension.findViewById(R.id.sabah);
TextView gunesView = (TextView) dimension.findViewById(R.id.gunes);
TextView ogleView = (TextView) dimension.findViewById(R.id.ogle);
TextView ikindiView = (TextView) dimension.findViewById(R.id.ikindi);
TextView aksamView = (TextView) dimension.findViewById(R.id.aksam);
TextView yatsiView = (TextView) dimension.findViewById(R.id.yatsi);


gunView.setText(gun);
imsakView.setText(imsak);
sabahView.setText(sabah);
gunesView.setText(gunes);
ogleView.setText(ogle);
ikindiView.setText(ikindi);
aksamView.setText(aksam);
yatsiView.setText(yatsi);
}


the update method gets called on user click. It occurs to me that the textview is redefined everytime
update()
is called. I tried defining TextView on the onCreate method but I kept on getting NPE.

Answer

You have to change like this way,

By doing this way, your textview will not be redefined everytime

change :

public class ViewFragment extends Fragment {

    public TextView gunView, imsakView, sabahView, gunesView, ogleView, ikindiView, aksamView, yatsiView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View inflatedView = inflater.inflate(R.layout.content_main, container, false);
        gunView = (TextView) inflatedView.findViewById(R.id.gun);
        imsakView = (TextView) inflatedView.findViewById(R.id.imsak);
        sabahView = (TextView) inflatedView.findViewById(R.id.sabah);
        gunesView = (TextView) inflatedView.findViewById(R.id.gunes);
        ogleView = (TextView) inflatedView.findViewById(R.id.ogle);
        ikindiView = (TextView) inflatedView.findViewById(R.id.ikindi);
        aksamView = (TextView) inflatedView.findViewById(R.id.aksam);
        yatsiView = (TextView) inflatedView.findViewById(R.id.yatsi);
        update("1", "2", "3", "4", "5", "5", "6", "7", "8", "9");  //remove view from here
        // Inflate the layout for this fragment
        return inflatedView;
    }

    public void update(String tarih, String hicri, String gun, String imsak, String sabah, String gunes, String ogle, String ikindi, String aksam, String yatsi) {
        gunView.setText(gun);
        imsakView.setText(imsak);
        sabahView.setText(sabah);
        gunesView.setText(gunes);
        ogleView.setText(ogle);
        ikindiView.setText(ikindi);
        aksamView.setText(aksam);
        yatsiView.setText(yatsi);
    }
}
Comments