I have a spinner of cities with default value "Other City". If the user can't find his city, when he selects Other City, a textInput (Your city) is shown. When the user enters his city name there and Clicks the button Register, the getText().toString() doesn't return the value. And also when the field is empty even though I have written validation code, it doesn't work.
Log.i("cities[0]", "("+cities[0]+")");
Log.i("regOtherCity", "("+(regUserOtherCity.getText().toString())+")");
if (userCity.equals(cities[0]) && TextUtils.isEmpty(regUserOtherCity.getText().toString()) )
{
error = true;
Log.i("checkerror", "("+cities[0]+")");
regUserOtherCity.setError("Enter Your City");
regUserOtherCity.requestFocus();
}
else if (userCity.equals(cities[0]))
userCity = regUserOtherCity.getText().toString();
Log.i("cities[0]", "("+cities[0]+")");
Log.i("regOtherCity", "("+userCity+")");
I have tried debugging by using Logcat, the output is here for the time when the user enters 'my city' in the text input.
I/cities[0]: (Other City)
I/regOtherCity: (my city)
I/cities[0]: (Other City)
I/regOtherCity: ()
Edited:----------------------------- On inspection, I found out that the value returned by userCity is empty.
here is the code that gets value of userCity..for some reasons it's not working.
regCitySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (String.valueOf(parent.getItemAtPosition(position)).equals(cities[0])) {
regOtherCityLayout.setVisibility(View.VISIBLE);
userCity = regUserOtherCity.getText().toString();
Log.i("onItemSel userCity", "("+userCity+")");
} else {
regOtherCityLayout.setVisibility(View.GONE);
userCity = String.valueOf(parent.getItemAtPosition(position));
}
}