【问题标题】:Setting edittext inside Recyclerveiw在 Recyclerview 中设置 edittext
【发布时间】:2017-11-20 12:12:04
【问题描述】:

我有一个包含 edittext 和 textview 的 recyclerview,并且我在 recyclerview 外面有一个按钮。我想在单击按钮 (edittext.setError("required")) 时验证编辑文本,为此我使用了 addTextChangedListener 并在 onTextChanged() 方法中将 charSequence 存储在 Hashmap 中。下面是我的 onBindViewHolder

    @Override
public void onBindViewHolder(ViewHolder holder, final int position) {



    String label = mData.get(position).getLabel();
    holder.label.setText(label);

    holder.label_input.setTag(position);

    holder.label_input.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
          //  Log.w("ontextchanged","i = "+i+" i1 = "+i1+" i2 = "+i2+""+"char seq "+charSequence.toString()+"position = "+position);
            stringHashMap.put(""+position,""+charSequence);

        }

        @Override
        public void afterTextChanged(Editable editable) {

        }
    });


}

在点击按钮的活动中,根据位置获取存储的值,下面是点击活动按钮

    submit.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {


        for (int i = 0; i< TimeSheetDetailAdapter.stringHashMap.size(); i++) {

            //Entered edittext value from hashmap
            String data = TimeSheetDetailAdapter.stringHashMap.get(""+i);


            }

    }
});

我根据位置正确获取数据中输入的edittext值,但问题是我想验证每个edittext,如果数据为空,我想用edittext.setError(“required”)设置特定的edittext ),为此我已经尝试过

 if(recyclerView.findViewHolderForLayoutPosition(i) instanceof TimeSheetDetailAdapter.ViewHolder){
                    TimeSheetDetailAdapter.ViewHolder childHolder = (TimeSheetDetailAdapter.ViewHolder) recyclerView.findViewHolderForLayoutPosition(i);

                    childHolder.label_input.setError("required");

但是它将错误设置为仅在屏幕上可见的项目,即只有当编辑文本可见时才会设置,不可见的项目不会设置。我知道 recyclerview 的概念,我也尝试过 findViewHolderForAdapterPosition(),但没有帮助,请帮我解决这个问题,我如何访问不可见的视图并设置 eroor 或任何其他方式。

提前谢谢你

【问题讨论】:

    标签: android android-recyclerview android-viewholder


    【解决方案1】:

    您可以分别按位置创建要在回收站列表上显示的消息的数组列表。

    并访问该数组列表以将其设置在标签中。我认为这可能是唯一的解决方案。因为您无权访问不可见的列表项。

    【讨论】:

    • 感谢您的回复,我会尝试并回帖
    • 。 .谢谢,我试过了,也是解决办法之一
    【解决方案2】:

    我最终得到了一个更好的解决方案

     if (data.equals("empty") || data.equals("")) {
    
                            isAllDataFilled = false;
                            //Scrolling to the respective position
                            linearLayoutManager.scrollToPositionWithOffset(i, 0);
    
                            //giving delay of 200ms to get the view and then setting error for that
                            new Handler().postDelayed(new Runnable() {
    
                                @Override
                                public void run() {
    
                                    TimeSheetDetailAdapter.ViewHolder childHolder = (TimeSheetDetailAdapter.ViewHolder) recyclerView.findViewHolderForLayoutPosition(i);
                                    childHolder.label_input.setError("error");
                                }
                            }, 200);
    
    
                            enteredDataList.clear();
                            break;
                        }
    

    谢谢

    【讨论】:

      猜你喜欢
      • 2020-12-24
      • 2021-03-02
      • 2015-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-13
      • 2016-09-04
      • 1970-01-01
      相关资源
      最近更新 更多