0

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));
            }
        }
  • So the first output has "my city" but on evaluation it's blank? – zgc7009 Apr 21 '19 at 12:46
  • When I entered 'my city' as text input in the form and after clicking the Register button that was the output I displayed in the Logcat to check. – Euclid Stonna Apr 21 '19 at 15:38
  • I'm not sure if I'm correctly reconciling your description with that code, but it looks like `userCity.equals(cities[0])` was never true, though it seems you expect that it is. – Mike M. Apr 21 '19 at 15:51
  • just updated the question. Can you please have a look. – Euclid Stonna Apr 21 '19 at 16:17
  • Your userCity variable is what's off. userCity is null (or an empty string) and so the evaluation do the if/else if is never true. Check the way you are setting userCity, that is what's getting messed up. – zgc7009 Apr 21 '19 at 16:59

1 Answers1

0

You're assigning userCity the value of the EditText inside onItemSelected. So if the user select Other City, the EditText value is assigned to userCity in the listener instead of the value of the EditText.

Simply assign the spinner value and use the if condition to set the visibility of the EditText.

userCity = String.valueOf(parent.getItemAtPosition(position));

Should work fine when you press the button then.

Usman Rafi
  • 316
  • 1
  • 2
  • 12
  • Make the following changes in `onItemSelected` remove this line `userCity = regUserOtherCity.getText().toString();` `userCity = String.valueOf(parent.getItemAtPosition(position));` move this line out of the if condition – Usman Rafi Apr 21 '19 at 16:25
  • By default, the spinner value is 'Other City'. If the user selects Other City a textInput is shown and regUserOtherCity should get the value of the User Input. this is where the user input value is not coming. – Euclid Stonna Apr 21 '19 at 16:35
  • When you execute it, does it go inside this condition `else if (userCity.equals(cities[0]))` ? – Usman Rafi Apr 21 '19 at 16:43
  • No, because i found out that userCity is empty. – Euclid Stonna Apr 21 '19 at 16:51
  • Can you please make the changes I suggested and let me know if it worked? – Usman Rafi Apr 21 '19 at 17:02
  • Heartly thanks for your answer. It worked. Sorry that I didn't get it sooner. – Euclid Stonna Apr 21 '19 at 17:07
  • No worries, you're welcome. I see you're new to SO. If you feel an answer solved the problem, please mark it as 'accepted' by clicking the green check mark. This helps keep the focus on older SO which still don't have answers. – Usman Rafi Apr 21 '19 at 17:18