0

This piece of code gets resources from a list and set errors for each field on a registration form, one of them is a spinner, so when the error is on the spinner, it fails due to the fact that the spinner is not a subclass of TextView. How can I get this code deal with the spinner in an elegant way?

// set errors
        for (Map.Entry<String, List<String>> error : errors.entrySet()) {
            int id = getActivity().getResources().getIdentifier("edit_" + error.getKey(), "id", getActivity().getPackageName());
            //TODO this codes doen't manage spinner errors! (checkboxes and editext are both TextView)
            TextView edit = (TextView) getActivity().findViewById(id);
            if (!error.getValue().isEmpty()) {
                edit.setError(error.getValue().get(0));
            }
        }
avafab
  • 1,601
  • 3
  • 20
  • 38

2 Answers2

2

You can check if the current view is an instance of a textview. If it is, then go ahead and cast it to a TextView, and perform whatever operations you were performing over it. Else, handle checkboxes/spinner/whatever in any way you'd like.

 for (Map.Entry<String, List<String>> error : errors.entrySet()) {
        int id = getActivity().getResources().getIdentifier("edit_" + error.getKey(), "id", getActivity().getPackageName());

        View edit = getActivity().findViewById(id);
        if (!error.getValue().isEmpty() && edit instanceof TextView) {
             TextView editTextView = (TextView)edit;
             editTextView.setError(error.getValue().get(0));
        }
    }
Urban
  • 2,201
  • 24
  • 52
  • Hi Urban, this seems that will jump the spinner fields, isn't it? I actually would like to display errors on the spinners as well as on the editText views using the same compact code. – avafab Apr 24 '15 at 08:38
  • Yes, this will skip the spinners. You'll have to do a bit more work to have a `setError` method for your spinner. Follow this link to see how to do that: http://stackoverflow.com/questions/3749971/creating-a-seterror-for-the-spinner Once that is implemented, you may have to add another if condition for your spinner. – Urban Apr 24 '15 at 08:42
  • Ok and this code (as it is) gives a warning while trying to cast a variable to itself. It also give an error on setError method because it doesn't convert "edit" variable of type View into "edit" variable of type "TextView". – avafab Apr 24 '15 at 08:48
  • What's the error? I'm able to cast a View to a TextView without any problems. You can safely ignore the warnings here, however, if you'd like to fix those warnings, you can just create another variable, cast the View to a TextView, and then assign this to the new variable. – Urban Apr 24 '15 at 08:56
  • that's what I did because if I leave like that it gave me an error on setErrors and a warning in the casting line. – avafab Apr 24 '15 at 08:59
  • ok, so are there still errors? Or the error was fixed by assigning it to a new variable? – Urban Apr 24 '15 at 09:01
  • the error has been fixed by assigning it to a new variable. – avafab Apr 24 '15 at 09:03
  • Cool, I'll make the change in the answer. – Urban Apr 24 '15 at 09:03
0

That's how I solved it, many thanks to Urban

 // set errors
        for (Map.Entry<String, List<String>> error : errors.entrySet()) {
            int id = getActivity().getResources().getIdentifier("edit_" + error.getKey(), "id", getActivity().getPackageName());
            View edit = getActivity().findViewById(id);

            if (!error.getValue().isEmpty() && edit instanceof TextView) {
                TextView mTextView;
                mTextView = (TextView)edit;
                mTextView.setError(error.getValue().get(0));
            } else if (!error.getValue().isEmpty() && edit instanceof Spinner){
                Spinner mSpinner;
                mSpinner = (Spinner)edit;
                ((TextView)mSpinner.getChildAt(0)).setError(error.getValue().get(0));
            }
        }
Community
  • 1
  • 1
avafab
  • 1,601
  • 3
  • 20
  • 38