skip to Main Content

I have created a dropdown using a custom layout and AutoCompleteTextView.
On submit button click I am checking if the user selected any items or not from that dropdown.

But even if an item is selected, a validation error is occurring on the validation check for the first time instead of no error.

The below code is for setting up my dropdown.

//Ambulance Types Dropdown Data
            ambulanceTypes = context.getResources().getStringArray(R.array.ambulance_types);
            ambulanceAdapter = new ArrayAdapter<>(context, R.layout.dropdown_item, ambulanceTypes);
            ambulanceDropdown = findViewById(R.id.autoCompleteTextView1);
            ambulanceDropdown.setAdapter(ambulanceAdapter);

I created a String variable to get the selected value in it as shown in the code below.

String ambulanceTypesValue;
ambulanceDropdown.setOnItemClickListener((adapterView, view, position, l) -> ambulanceTypesValue = ambulanceAdapter.getItem(position));

Below is the validation method I am calling on button click.

private boolean checkAmbulanceTypes() {
        ambulanceDropdown.setOnItemClickListener((adapterView, view, position, l) -> ambulanceTypesValue = ambulanceAdapter.getItem(position));
        if (ambulanceTypesValue == null) {
            ambulanceDropdown.requestFocus();
            ambulanceDropdown.setError("Please select an ambulance.");
            return false;
        } else {
            ambulanceDropdown.setError(null);
            return true;
        }
    }

Any suggestions on how to accomplish this?

2

Answers


  1. Chosen as BEST ANSWER

    Issue resolved by adding this code line repeatedly into the onResume() method.

    ambulanceDropdown.setOnItemClickListener((adapterView, view, position, l) -> ambulanceTypesValue = ambulanceAdapter.getItem(position));
    

    DETAILS: That line code is getting selected value from the dropdown and I repeated that line code in the onResume() method to resolve the issue.


  2. ok look my friend you write little information, but I will try to help you.

    First:
    Remove ambulanceDropdown.setOnItemClickListener from your funcation and pass string as a parameter

    private boolean checkAmbulanceTypes(String Types) {
            if (Types == null) {
                ambulanceDropdown.requestFocus();
                ambulanceDropdown.setError("Please select an ambulance.");
                return false;
            } else {
                ambulanceDropdown.setError(null);
                return true;
            }
        }

    Second:

    String ambulanceTypesValue;
    ambulanceDropdown.setOnItemClickListener((adapterView, view, position, l) -> ambulanceTypesValue = ambulanceAdapter.getItem(position));

    Remove this String from code above and in ambulanceDropdown.setOnItemClickListener put your funcation to be your code

        ambulanceDropdown.setOnItemClickListener((adapterView, view, position, l) -> checkAmbulanceTypes(ambulanceAdapter.getItem(position)));

    now your adapter will work fine and you will get a boolean value in setOnItemClickListener.

    Note:
    if you prefer use String ambulanceTypesValue to get the item which uses just change to

     String ambulanceTypesValue;
        ambulanceDropdown.setOnItemClickListener((adapterView, view, position, l) -> ambulanceTypesValue = ambulanceAdapter.getItem(position);
        );
        
        /*and on click button*/
        
        onclick{
        checkAmbulanceTypes(ambulanceTypesValue);
        ambulanceTypesValue=null;/*to clear value*/
        }
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search