EditText values in a RecyclerView gives same value after 5th position

拟墨画扇 提交于 2019-12-01 08:12:08

RecyclerView creates only as many view holders as are needed to display the on-screen portion of the dynamic content, plus a few extra. As the user scrolls through the list, the RecyclerView takes the off-screen views and rebinds them to the data which is scrolling onto the screen

// Items for recycler View 
//binding data
private ArrayList<Data> Items;

//HashMap to store editText text afterTextChanged
    //each editext in RecyclerView contains Unique Key And Value
    private HashMap<String,String> sourceData=new HashMap<>();

Refer This RecyclerView Program

public class DataRecyclerView extends RecyclerView.Adapter<DataRecyclerView.DataViewHolder> {

    private Context context;
    // Items for recycler View 
    //binding data
    private ArrayList<Data> Items;
    private LayoutInflater layoutInflater;

    //HashMap to store editText text afterTextChanged
    //each editext in RecyclerView contains Unique Key And Value
    private HashMap<String,String> sourceData=new HashMap<>();

    DataRecyclerView(Context context,ArrayList<Data> Items)
    {
        this.Items=Items;
        this.context=context;
        layoutInflater=LayoutInflater.from(context);
    }

    @NonNull
    @Override
    public DataViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    //DataBinding is used to bind Data
        DataViewHolderBinding binding= DataBindingUtil.inflate(layoutInflater, R.layout.template,parent,false);
        return new DataViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull DataViewHolder holder, int position) {
    //Pass Data pojo to  Holder
        holder.bindData(Items.get(position));
    }

    @Override
    public int getItemCount() {
        return Items.size();
    }


    class DataViewHolder extends RecyclerView.ViewHolder {




        private EditText amount;


         DataViewHolder(DataViewHolderBinding itemView) {
            super(itemView.getRoot());
            binding=itemView;
            amount=binding.PayAmount;
        }

         void bindData(Data data)//Data pojo for DataBinding
        {
            if(binding!=null) {
            // data will  automatically set to textViews In DataBinding  
            binding.setData(data);



               //every time data binds to views 
               //get text of respective edittext and assign to that current edittext

                if (sourceData.containsKey((String.valueOf(getAdapterPosition())))) {// checks current editText key  is availible or not
                      if (data.getIDNumber().equals(Items.get(getAdapterPosition()).getData.getIDNumber())) { // 
                        amount.setText(sourceData.get((String.valueOf(getAdapterPosition()))).getAmount());
                }else
                    {
                           if (data.getIDNumber().equals(Items.get(getAdapterPosition()).getData.getIDNumber())) { // 
                             amount.setText(null);
                    }



                amount.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) {


                    //when user enter text into edittext 
                    //editetext key already availible  in sourceData
                    // then update the value to hashmap sourceData
                        if (sourceData.containsKey(String.valueOf(getAdapterPosition()))) {
                            if (data.getIDNumber().equals(Items.get(getAdapterPosition()).getData.getIDNumber())) { // 
                                if (!s.toString().trim().isEmpty()) {
                                    if (!s.toString().trim().equals("0")) {
                                        sourceData.put(String.valueOf(getAdapterPosition()),s.toString().trim());

                                    } else {
                                        sourceData.put(String.valueOf(getAdapterPosition()), s.toString().trim();

                                    }
                                } else {
                                    sourceData.put(String.valueOf(getAdapterPosition()),null );
                                }
                            }

                        }
                        else {

                        //when user enter text into  edittext for the first time
                        //check for current Data pojo IDNumber with getAdapterPosition Items Data pojo IDNumber
                        //if it equal 
                        //then we  store text into hashmap for specific edittext by using adapterPosition as key

                            if (data.getIDNumber().equals(Items.get(getAdapterPosition()).getData.getIDNumber())) { // 
                                if (!s.toString().trim().isEmpty()) {
                                    if (!s.toString().trim().equals("0")) {
                                        sourceData.put(String.valueOf(getAdapterPosition()),s.toString().trim());

                                    } else {
                                        sourceData.put(String.valueOf(getAdapterPosition()), s.toString().trim();

                                    }
                                } else {
                                    sourceData.put(String.valueOf(getAdapterPosition()),null );
                                }
                            }
                        }
                    }
                });

            }
        }




    }

}

### Image Reference I can't show proper Image but Its Look Like this

Phan Van Linh

The rows in RecyclerView is reusing while scrolling. So you need to create an array for save each EditText value
Then addTextChangedListener to your EditText to save the EditText value while you input

public void onBindViewHolder(SelectItemAdapter.ItemHolder holder, int position) {
    ...
    holder.numPicker.setText(arrayForSaveEditTextValue[position]);
    holder.numPicker.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
            // TODO Auto-generated method stub
        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                                      int arg3) {
            // TODO Auto-generated method stub
        }

        @Override
        public void afterTextChanged(Editable arg0) {
            // TODO Auto-generated method stub
            arrayForSaveEditTextValue[position] = arg0.toString();
        }
    });
    ...
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!