jdelaporte
jdelaporte

Reputation: 127

Android editText string returns false rather than "" java

I am pulling string values from Android editText elements, but the text is coming in as "false" rather than as "". Is this expected behavior? All the posts I have read here lead me to think that it should be "". I would rather have the empty values be "" since they are going into a HashMap to post over HTTP. I tried removing "android:hint" from one of the fields (in case that was causing the behavior), but it still printed as false. I am trying to figure out what is happening and what is actually expected.

Here is my code to set the strings at Class level of the Activity

String uFirstName = "";
String uLastName = "";

And in onClick()

uFirstName = getText(R.id.editTextFirstName).toString();
uLastName = getText(R.id.editTextLastName).toString();
System.out.println(uFirstName);
System.out.println(uLastName);
System.out.println(uAddress);
System.out.println(uCity);

LogCat prints out

false
false
false
false

XML for these editText elements

        <EditText
            android:id="@+id/editTextLastName"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/RadioGroup1"
            android:layout_toRightOf="@+id/editTextFirstName"
            android:ems="10"
            android:hint="LastName"
            android:inputType="textPersonName" />

        <EditText
            android:id="@+id/editTextAddress"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/editTextLastName"
            android:ems="10"
            android:hint="Your Street Address"
            android:inputType="textPostalAddress" />

        <EditText
            android:id="@+id/editTextCity"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/editTextAddress"
            android:ems="10"
            android:hint="City"
            android:inputType="textPersonName" />

Upvotes: 2

Views: 172

Answers (2)

Nidhish Krishnan
Nidhish Krishnan

Reputation: 20741

Try this

EditText first= (EditText)findViewById(R.id.editTextFirstName);
EditText last = (EditText)findViewById(R.id.editTextLastName);

String firstnamevalue=first.getText().toString();
String lastnamevalue=last .getText().toString();

This is how you get the value from EditText in Android

Upvotes: 1

Josh Engelsma
Josh Engelsma

Reputation: 2646

EditText uFirstName1 = (EditText)findViewById(R.id.editTextFirstName);
EditText uLastName1 = (EditText)findViewById(R.id.editTextLastName);
uFirstName = uFirstName1.getText().toString();
uLastName = uLastName1.getText().toString();

Give this a try, Note: you will have to import the android EditText, or it wont compile.

Upvotes: 2

Related Questions