Why I can not call the method inside Fragment

advertisements

Why is it not possible to call the checkButtonAddmethod in my Fragment?

public class AddDataFragment extends Fragment {
    DbHelper mydb;

    Button buttonadd;
    Button buttondelete;

    private EditText inputLabel;

    //Validator boolean
    public boolean labelOk;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_add_data, container, false);

        final Context context = getContext();
        mydb = new DbHelper(context);

        [...]

        //Label
        inputLabel = (EditText) view.findViewById(R.id.editText_label);
        inputLabel.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

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

            @Override
            public void afterTextChanged(Editable s) {
                Pattern p = Pattern.compile("^[A-Z]{3}-[0-9]{4}$");
                Matcher m = p.matcher(s);

                labelOk = m.find();
                Log.d("ButtonAdd?", String.valueOf(labelOk));
            }
            checkButtonAdd(); // <---------------
        });

         [...]

        return view;
    }

    public void checkButtonAdd(){
        if (labelOk){
            buttonadd.setEnabled(true);
        }else{
            buttonadd.setEnabled(false);
        }
    };
}

I thought this should work because I used it in another Application. But in the MainActivitiy could this be the mistake and if yes, how can I solve it?


You have to call that method from one of the callback methods using this keyword, to get the instance of enclosing Fragment. For instance..

@Override
public void afterTextChanged(Editable s) {
    // Your Statements
    AddDataFragment.this.checkButtonAdd();
}

No need to declare the method as static. In fact, declaring it static will be a piece of poor code