Home > Software design >  onClickListener calls TextWatcher: Illegal forward reference
onClickListener calls TextWatcher: Illegal forward reference

Time:12-07

I'm working on Radio Buttons and I want each RadioButton to re-call the TextWatcher in every onClick to update my TextView but it doesn't work.

all of this code is inside a Fragment and I got only one Activity

RadioButton:

View.OnClickListener radio1Clicked = v ->{
        radioButton1.addTextChangedListener(textWatcher); //error under textWatcher : Illegal forward reference
        //More Operations Running here ... };

View.OnClickListener radio2Clicked = v ->{ 
        radioButton2.addTextChangedListener(textWatcher)};//error under textWatcher: Illegal forward reference
        //More Operations Running here ... };

View.OnClickListener radio3Clicked = v ->{ 
        radioButton2.addTextChangedListener(textWatcher)};//error under textWatcher: Illegal forward reference
        //More Operations Running here ... };

Text Watcher:

    TextWatcher textWatcher= new TextWatcher(){
        //Declarations and other stuff
                // RadioButton1
                if (radioButton1.isChecked()) {
                    basePrintable = 50;
                    calculatePcs = (int) (50 / sqIn);
                    iPieces.setText(String.valueOf(calculatePcs ));
                    if (!checker)//check Switch {
                        iPriceOut.setText("150.0");
                    } else if (checker) //re-check Switch{
                        iPriceOut.setText("50.0");
                    }
                }
                // RadioButton1
                if (radioButton1.isChecked()) {
                    basePrintable = 50;
                    calculatePcs = (int) (50 / sqIn);
                    iPieces.setText(String.valueOf(calculatePcs ));
                    if (!checker)//check Switch {
                        iPriceOut.setText("150.0");
                    } else if (checker) //re-check Switch{
                        iPriceOut.setText("50.0");
                    }
                }
                // RadioButton2
                if (radioButton1.isChecked()) {...}
                // RadioButton3
                if (radioButton1.isChecked()) {...}


     basePcs = Integer.parseInt(iPieces.getText().toString());
                baseSheet = 
     Integer.parseInt(iShit.getText().toString());
                basePrice = 
     Double.parseDouble(iPriceOut.getText().toString());
    }

I know it could be called cause I'm already doing it here: (and it works)

public void onCheckedChanged(CompoundButton switchInput, boolean isChecked) {
        switchInput.addTextChangedListener(size);
        if (!isChecked) {
            switchInput.setText("...");
            radioButton1.setText("...");
            radioButton2.setText("...");
            radioButton3.setText("...");
        } else {
            switchInput.setText("...");
            radioButton1.setText("...");
            radioButton2.setText("...");
            radioButton3.setText("...");
        }
    }

I'm a complete noob and only following along Tutorials and stitching this app for myself. this is the last problem I have to solve cause everything else is more or less done.

Thanks for the Help

CodePudding user response:

I think the problem is that you can't reference the object that was clicked inside, instead use the view that is passed

View.OnClickListener o1 = v ->{
        iOpt1.addTextChangedListener(size); //error: Illegal Forward Reference
        if (iOpt1.isChecked()) {
            iOpt2.setChecked(false);
            iOpt3.setChecked(false);
        }
        iOpt1.setChecked(true);
    };

change to

View.OnClickListener o1 = v ->{
        v.addTextChangedListener(size); <--change here //error: Illegal Forward Reference
        if (v.isChecked()) { <--change here
            iOpt2.setChecked(false);
            iOpt3.setChecked(false);
        }
        v.setChecked(true);<--change here
    };

using v as the view was clicked

CodePudding user response:

I just needed to update or refresh the TextWatcher by re-assigning it's own content back to an EditText it was injected on, this will cause an update and will run the TextWatcher since basically it has been updated.

 View.OnClickListener o1 = v ->{
        if (iOpt1.isChecked()) {
            iOpt2.setChecked(false);
            iOpt3.setChecked(false);
        }
        iOpt1.setChecked(true);

        //SOLUTION: save the value on where the **TextWatcher** is injected then re-assign the same value you saved

        refesher = iLength.getText().toString();
        iLength.setText(refesher);

        //This will cause an update to the **TextWatcher** and will run the code again

    };

TextWatcher size = new TextWatcher() {...}

Here's more Context

public class PrintFragment extends Fragment{

EditText iLength, iHeight;
RadioButton iOpt1, iOpt2, iOpt3;
String refresher;

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

    iLength = V.findViewById(R.id.length);
        iHeight = V.findViewById(R.id.height);
        iOpt1 = V.findViewById(R.id.opt1);
        iOpt2 = V.findViewById(R.id.opt2);
        iOpt3 = V.findViewById(R.id.opt3);
        

        iLength.addTextChangedListener(size);
        iHeight.addTextChangedListener(size);
        iOpt1.setOnClickListener(o1);
        iOpt2.setOnClickListener(o2);
        iOpt3.setOnClickListener(o3);
     return;
     }

 View.OnClickListener o1 = v ->{
        if (iOpt1.isChecked()) {
            iOpt2.setChecked(false);
            iOpt3.setChecked(false);
        }
        iOpt1.setChecked(true);
        refesher = iLength.getText().toString();
        iLength.setText(refesher);
    };

TextWatcher size = new TextWatcher() {...}
}
  • Related