Validate RecyclerView content

本秂侑毒 提交于 2019-12-11 15:44:18

问题


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?


回答1:


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;
    }



回答2:


Maybe that library is what you need https://github.com/Link184/KidAdapter

Here is a short example how you can validate data:

val adapter : TypedKidAdapter = recyclerView.setUp {
    withViewType {
        withItems(mutableListOf("one", "two"))
        withLayoutResId(android.R.layout.list_content)
        withContentComparator<String> { oldObject, newObject ->
            insertNewViewType(newItems)
            oldObject.compareTo(newObject)
        }
        withItemsComparator<String> { oldObject, newObject ->
            oldObject.equals(newObject).also {
                updateItemsFromViewType(someNewItems)
            }
        }
        bind<String> {
            //ui logic
        }
    }

    withViewType {
        //another viewtype config
    }
}

fun insertNewViewType(newItems: MutableList<Any>) {
    adapter restructure {
        insertTop {
            withItems(newItems)
            withLayoutResId(android.R.layout.list_content)
            withItemsComparator<Any> {
                ...
            }
            bind<Any> { 
                //UI logic
            }
        }
    }
}

fun updateItemsFromViewType(newItems: String) {
    adapter update {
        insertBottom(newItems)
    }
}


来源:https://stackoverflow.com/questions/56054259/validate-recyclerview-content

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!