-1

I am currently attempting to clear the sensor values in a fragment, that are constantly being updated, however the clear button that I have set does nothing, even when I have paused the sensors. Have I correctly setup the onViewCreated?

I have also attempted to setup the textviews in onCreateView to no success. I get no errors whatsoever when I press the clear button, the values just don't clear.

Any pointers would be greatly appreciated!

public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);


    mPitchView = (TextView)view.findViewById(R.id.textView4);
    mRollView = (TextView)view.findViewById(R.id.textView5);
    mAzimuthView = (TextView)view.findViewById(R.id.textView6);
    mRadioGroup = (RadioGroup) view.findViewById(R.id.radioGroup);
    mRadioGroup.setOnCheckedChangeListener(this);

    view.findViewById(R.id.button_clear).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            onClear();

        }
    });   
}


public void onClear(){


    mPitchView.setText("");
    mRollView.setText("");
    mAzimuthView.setText("");
}

Also I have two other buttons start and stop which work perfectly fine. I think the issue is the textview not updating.

  • 1
    Log your onclick to see if there is an output in the logcat – Olayinka Jun 04 '16 at 04:20
  • 1
    Try initializing `mPitchView`, `mRollView`, etc outside of the onViewCreated method. Also inside of the onClear() method try setting `mPitchView.getText().clear();` – coletrain Jun 04 '16 at 04:21
  • Honestly, this code looks fine. Are you sure you are using the correct ids for the views? – OneCricketeer Jun 04 '16 at 04:32
  • Did you try to put a breakpoint on the onClear() method and check if it has been hit? – Lichader Jun 04 '16 at 04:48
  • @ cole For fragments, i think i have to initialise the views inside the method? Correct me if I'm wrong. @ cricket yes I am sure. @ Lichader yes I have tried, the buttons are being hit and method is called. – oompagoompa Jun 04 '16 at 05:13
  • this should work since you say onclear gets called. the only reason I can see maybe are altering these values later on onresume or somewhere else? – z3n105 Jun 04 '16 at 08:35
  • @aloupas it shouldn't be , i have tried using onstop and onpause to ensure sensor values are not updated , following with onclear, however text still remains the same. – oompagoompa Jun 04 '16 at 08:41

1 Answers1

0
use  Handler     

 Handler txtsettext = new Handler(Looper.getMainLooper());
                            txtsettext.post(new Runnable() {
                                public void run() {
                                   mPitchView.setText("");
                                   mRollView.setText("");
                                   mAzimuthView.setText("");   

                                }
                            });
Gil Snovsky
  • 210
  • 2
  • 6
  • Hi, sorry I am relatively new to the concepts of handlers, how would I incorporate it into my code? – oompagoompa Jun 04 '16 at 15:08
  • public void onClear(){ Handler txtsettext = new Handler(Looper.getMainLooper()); txtsettext.post(new Runnable() { public void run() { mPitchView.setText(""); mRollView.setText(""); mAzimuthView.setText(""); } }); } – Gil Snovsky Jun 04 '16 at 15:14
  • Have tried again, works now , turns out there was a runnable causing the display to be constantly updated with the last sensor result. My fault. Thanks – oompagoompa Sep 09 '16 at 15:08