验证RecyclerView内容 [英] Validate RecyclerView content

查看:84
本文介绍了验证RecyclerView内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为无需重复有关如何设置RecyclerView的确切描述.此处描述: Android-具有各种动态功能的RecyclerView内容,每个项目都不同

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

简而言之,我的Recyclerview可能包含各种控件,例如编辑文本,微调框等.然后,recyclerview放置在Activity布局中,始终以嵌套滚动视图作为其父级进行扩展.当用户在我的活动中单击确认"按钮时,应验证回收者视图的内容-例如,如果任何特定项目包含需要为非空的textview,则活动不应结束,并且应在该特定编辑文本中显示标准错误标记

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.

在回收者视图中,我创建了validate方法,该方法应返回truefalse,因此我将知道验证结果.但是,我不知道如何从适配器访问特定的recyclerview项的内容,这是我的方法(在这种情况下,将文本控件的类型设置为email):

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

itemsList<Object>,它包含具有各种实际类型的所有recyclerview项,每种类型确定呈现的特定recylerview内容.当用户更改特定项目的控件时,items中的相应项目将分别进行更新,因此,调用validate时,所有项目都包含新数据(对于EmailParameter,它将是value字段已设置为文本类型)在编辑文本控件中.)

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.

你有什么主意吗?

推荐答案

有效的解决方案-使用notifyDatasetChanged:

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

这篇关于验证RecyclerView内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆