Ad

Validate RecyclerView Content

I think there is no need to repeat the exact description of how my RecyclerView is set. This is described here: Android - RecyclerView with various dynamic content, different for each item

In short, my Recycler View may contain various controls, like edit text, spinner, etc. Then, recyclerview is placed in Activity layout, always expanded with nested scroll view as its parent. When user hit "Confirm" button in my activity, recycler view's content should be validated - for example, if any particular item contains textview that needs to be nonempty, activity should not be finished and standard error mark inside that particular edit text should be shown.

Inside my recycler view, I created validate method and it should return true or false so I will know validation result. However, I don't know how to access particular recyclerview item's content from adapter, this is my method (in this case, edit text control with type set to email):

public boolean validate()
    {
        for(Object item : items)
        {
            Parameter p = (Parameter)item;

            switch (p.type)
            {
                case Parameter.EMAIL_PARAM:
                {

                    String email = ((EmailParameter)p).value;
                    /*validate email here, if invalid, return
                     false and set error indicatior for corresponding 
                     recyclerview item*/

                }
            }
        }


        return true;
    }

items is List<Object> and it contains all recyclerview items with various actual types, each type determines particular recylerview content rendered. When user changes particular item's control, the corresponding item from items will be updated respectively, so when validate called, all items contain fresh data (in case of EmailParameter it will be value field already set to text typed in edit text control).

As I can validate final values easily, I don't know, how to update corresponding recycler view item control to show an error.

Do you have any ideas?

Ad

Answer

Working solution - use notifyDatasetChanged:

public boolean validate()
    {
        boolean allValid = true;

        for(Object item : items)
        {
            Parameter p = (Parameter)item;

            switch (p.type)
            {
                case Parameter.EMAIL_PARAM:
                {

                    String email = ((EmailParameter)p).value;
                    /*validate email here, if invalid, return
                     false and set error indicatior for corresponding 
                     recyclerview item*/

                    p.errorMessage = valid ? null:"Email is incorrect";
                    allValid &= valid;

                    break;

                }
               /*All remaining parameter types validaion*/
            }



        }


         if(!allValid) this.notifyDataSetChanged();
         return allValid;
    }
Ad
source: stackoverflow.com
Ad