3

How can I validate my Spinner Android Saripaar?

I have declared my Spinner in the following manner.

@Select(order = 8)
Spinner spin_country;

and this is my onValidationFailed() method.

@Override
public void onValidationFailed(View failedView, Rule<?> failedRule) {
    // TODO Auto-generated method stub

    String message = failedRule.getFailureMessage();
    if (failedView instanceof Spinner) {
        failedView.requestFocus();
        // What should i do here??
    } else {

    }
}
Ragunath Jawahar
  • 19,513
  • 22
  • 110
  • 155
Riju Thomas
  • 49
  • 1
  • 5

3 Answers3

4

If you want a simpler solution than provided by user3508814, just do the following (no need to track spinnerSelections):

Annotation used

@Select
Spinner spin_country;

onValidationFailed

    @Override
    public void onValidationFailed(List<ValidationError> errors) {
        for (ValidationError error : errors) {
            View view = error.getView();
            String message = error.getCollatedErrorMessage(this);

            // Display error messages
            if (view instanceof EditText) {
                ((EditText) view).setError(message);
            }
            else if (view instanceof Spinner) {
                ((TextView) ((Spinner) view).getSelectedView()).setError(message);
            }
        }
    }
Rafael Oliveira
  • 2,823
  • 4
  • 33
  • 50
1

Here is my solution.

Use a map to track spinners and their text views (if you have multiple).

private Map<View, TextView> spinnerSelections = new HashMap<View, TextView>();

Use OnItemSelectedListener to record changes to the spinner selection. This fires when the activity loads, so your default selection will be tracked.

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            spinnerSelections.put(parent, (TextView) view);
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });

Set errors on spinner's text view

@Override
public void onValidationFailed(List<ValidationError> errors) {
    for (ValidationError error : errors) {
        View view = error.getView();
        String message = error.getCollatedErrorMessage(this);

        if (view instanceof EditText) {
            ((EditText) view).setError(message);
        }
        else if (view instanceof Spinner) {
            spinnerSelections.get(view).setError(message);
        }
    }
}

I set my spinners to not be focusable so I don't see any error messages but I do get the red exclamation point.

Hope this is helpful.

0

In your specific case, Saripaar the Spinner validation will fail if the first item (index 0) is selected in your Spinner.

Considering these are the items on your Spinner.

-- Country --
Albania
France
India
Sri Lanka
...

The validation will fail if -- Country -- is selected, because it is the first item (index 0) on the Spinner.

Ragunath Jawahar
  • 19,513
  • 22
  • 110
  • 155