Android RecyclerView -Multiple Edittext change simultaneously

无人久伴 提交于 2019-12-06 07:43:06

That happens because the views inside a RecyclerView are being recycled. Inside the onBindViewHolder you need to set the specific texts to the EditTexts at the specific position.

  1. Initialize your Views inside the ViewHolder and add the TextWatchers there as the whole philosophy of the RecyclerView is to reuse the views :

    public static class ViewHolder extends RecyclerView.ViewHolder {
    
        EditText txt;
        EditText txt2;
        EditText txt3;
        EditText txt4;
    
        TextView serNoTxt;
    
        public ViewHolder(LinearLayout mCardView) {
            super(mCardView);
            txt = (EditText) mCardView.findViewById(R.id.ip1_text);
            txt2 = (EditText) mCardView.findViewById(R.id.ip2_text);
            txt3 = (EditText) mCardView.findViewById(R.id.ip3_text);
            txt4 = (EditText) mCardView.findViewById(R.id.ip4_text);
    
            setNoTxt = (TextView) mCardView.findViewById(R.id.serNo);
    
            txt.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    
                }
    
                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
    
                }
    
                @Override
                public void afterTextChanged(Editable s) {
                    data[getAdapterPosition()][0] = s.toString();
                    Log.d("DATA" + getAdapterPosition() + "0", s.toString());
                }
                });
            txt2.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    
                }
    
                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
    
                }
    
                @Override
                public void afterTextChanged(Editable s) {
                    data[getAdapterPosition()][1] = s.toString();
                    Log.d("DATA" + getAdapterPosition() + "1", s.toString());
                }
            });
            txt3.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    
                }
    
                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
    
                }
    
                @Override
                public void afterTextChanged(Editable s) {
                    data[getAdapterPosition()][2] = s.toString();
                    Log.d("DATA" + getAdapterPosition() + "2", s.toString());
                }
            });
            txt4.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    
                }
    
                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
    
                }
    
                @Override
                public void afterTextChanged(Editable s) {
                    data[getAdapterPosition()][3] = s.toString();
                    Log.d("DATA" + getAdapterPosition() + "3", s.toString());
                }
            });
        }
    }
    
  2. Set the appropriate text to the EditTexts in the onBindViewHolder :

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        holder.txt1.setText(data[position][0]);
        holder.txt2.setText(data[position][1]);
        holder.txt3.setText(data[position][2]);
        holder.txt4.setText(data[position][3]);
    
        holder.setNoTxt.setText(mDataset[position]);
    }
    

You updated the data without notifying the adapter. This could create inconsistencies. So after the data is changed you should call notifyItemChanged().

For example:

    @Override
    public void afterTextChanged(Editable s) {
        data[position][3] = s.toString();
        Log.d("DATA" + position + "3", s.toString());
        notifyItemChanged(position);
    }

One more suggestion I have is to reuse the code so you won't have to copy paste so much and fix in many places (for example, all of your TextWatcher objects are basically the same).

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