wasimsandhu
wasimsandhu

Reputation: 4555

EditText getText().toString() not working

I've been Googling for a while, but I can't find anything that solves my problem. I have some code in a nested fragment class that looks like this:

String infinitive;

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

        // Grabs input from text field and places it in String infinitive
        final EditText mMainTextField = (EditText) rootView.findViewById(R.id.mainTextField);
        infinitive = mMainTextField.getText().toString();

        final Button mConjugateButton = (Button) rootView.findViewById(R.id.conjugateButton);
        mConjugateButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                boolean isEndingAr = infinitive.endsWith("ar");
                boolean isEndingEr = infinitive.endsWith("er");
                boolean isEndingIr = infinitive.endsWith("ir");

                // Check to see what kind of verb it is before conjugating
                if (isEndingAr) {
                    conjugateArVerb();
                } else if (isEndingEr) {
                    conjugateErVerb();
                } else if (isEndingIr) {
                    conjugateIrVerb();
                } else {
                    Toast testToast = Toast.makeText(getActivity(), "Could not conjugate the verb: " + infinitive, Toast.LENGTH_SHORT);
                    toast.show();
                }
            }

My problem is that this line doesn't seem to be working:

infinitive = mMainTextField.getText().toString(); 

The testToast only returns "Could not conjugate: " not the value I input into the text field. I would post the XML file and the rest of the code in MainActivity.java, but I think this my problem is specifically with the getText().toString() line.

Please help me figure this out. Thanks in advance.

Upvotes: 2

Views: 7045

Answers (3)

Michael Yaworski
Michael Yaworski

Reputation: 13483

You're setting infinitive to the value of the EditText content immediately after you instantiate the EditText object. However, you need it to obtain the value whenever the button is pressed because that's when you use the information.

So instead of this:

final EditText mMainTextField = (EditText) rootView.findViewById(R.id.mainTextField);
infinitive = mMainTextField.getText().toString();

// button listener

You should define infinitive inside the button listener, like this:

final EditText mMainTextField = (EditText) rootView.findViewById(R.id.mainTextField);

final Button mConjugateButton = (Button) rootView.findViewById(R.id.conjugateButton);
mConjugateButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        infinitive = mMainTextField.getText().toString();

        boolean isEndingAr = infinitive.endsWith("ar");
        boolean isEndingEr = infinitive.endsWith("er");
        boolean isEndingIr = infinitive.endsWith("ir");

        // ...

Upvotes: 5

Juan Cortés
Juan Cortés

Reputation: 21062

When you are executing this line infinitive = mMainTextField.getText().toString(); the user hasn't had the chance to enter anything in the EditText. The click event is the moment when you are sure that the user wants you to read that value. Moving that line to inside that method should fix it.

Upvotes: 1

CommonsWare
CommonsWare

Reputation: 1006584

Move:

infinitive = mMainTextField.getText().toString();

into the onClick() method. Right now, it will be empty, because the user has not typed anything by the time you execute that statement, microseconds after the EditText was created.

Upvotes: 1

Related Questions