简体   繁体   中英

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.

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);
    }
}

here's my thought, hope it works, I havent tried it myself. If you would want to define TextView and not redefine it over and over again, why not do it inside of your fragment class using ButterKnife after compiling with gradle option (compile 'com.jakewharton:butterknife:7.0.1').

    public class MyFragment extends Fragment {

        @Bind(R.id.textView)TextView tv;
        @Bind(R.id.textView)TextView tv2, tv3 etc;
    ...
    ...
        @Override
        OnActivityCreated(Bundle savedInstance){
          ButterKnife.Bind(getActivity());
   }
        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);

          tv.setText(String.format(Locale.US, "ABCD %d", i));
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM