Question

I have multiple String variables and EditText variables in my MainFragment. And for all the EditText, I want my String variables to get assigend the values after EditText's text changed. Currently this is how I do it:

private String itemName;
private String itemNumber;
private EditText itemNameET;
private EditText itemNumberET;
private void initEditTexts() {
    itemName = itemNameET.getText().toString();
    itemNumber = itemNumberET.getText().toString();
    itemNameET.addTextChangedListener(new TextWatcher() {
        @Override
        public void afterTextChanged(Editable s) {
            itemName = (String) s.toString();
        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1,
            int arg2, int arg3) {}

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
            int count) {}

    });

    itemNumberET.addTextChangedListener(new TextWatcher() {
        @Override
        public void afterTextChanged(Editable s) {
            itemNumber = (String) s.toString();
        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1,
            int arg2, int arg3) {}

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
            int count) {}

    });

I want to try remove the redundant code since I have a lot of EditTexts in the Activity. I was thinking of going along with create a CustomTextWatcher:

public class CustomTextWatcher implements TextWatcher {
    private String text;
    public CustomTextWatcher(String text) {
        this.text = text;
    }

    @Override
    public void afterTextChanged(Editable s) {
        text = (String) s.toString();
    }

    @Override
    public void beforeTextChanged(CharSequence arg0, int arg1,
        int arg2, int arg3) {}

    @Override
    public void onTextChanged(CharSequence s, int start, int before,
        int count) {}
}

But then this won't work because the outerclass variable is still not assigned the new value, since it is pass-by-value. How will I do something in Java that has the pass-by-reference effect, or what other ways I can do to pass the outerclass variable to the innerclass and that variable can get updated in innerclass?

Était-ce utile?

La solution

Remember that arrays get the values passed by the address and not by value. Maybe you should try something like:

public  class CustomTextWatcher implements TextWatcher {
    private String [] text;
    public CustomTextWatcher(String[] text) {
        this.text = text;
    }

    @Override
    public void afterTextChanged(Editable s) {
        text[0] = (String) s.toString();
    }

    @Override
    public void beforeTextChanged(CharSequence arg0, int arg1,
        int arg2, int arg3) {}

    @Override
    public void onTextChanged(CharSequence s, int start, int before,
        int count) {}
}

Of course you should always be aware of validating the array in size to avoid NullPointerException. Hope it suits your needs.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top