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
Issue resolved by adding this code line repeatedly into the
onResume()
method.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.
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
Second:
Remove this String from code above and in ambulanceDropdown.setOnItemClickListener put your funcation to be your code
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