Getting values from RecyclerView EditText?

前端 未结 5 1619
清酒与你
清酒与你 2021-01-05 12:51

I am struck in recyclerView,

\"image\"

Here the name and balance fields are coming from two different a

5条回答
  •  遥遥无期
    2021-01-05 13:21

    I think you are looking for a callback, which means whenever a number on one of the EditTexts is changed you want the total number change too. So first of all you need to add an interface,

    OnEditTextChanged Interface

    public interface OnEditTextChanged {
        void onTextChanged(int position, String charSeq);
    }
    

    Then you need too include this in the constructor of the adapter.

    In the Adapter.java

    private List mDataSet;
    private OnEditTextChanged onEditTextChanged;
    
    public Adapter(List myData, OnEditTextChanged onEditTextChanged) {
        mDataSet = myData;
        this.onEditTextChanged = onEditTextChanged;
    }
    

    In the onBindViewHolder of your Adapter you need to set a listener for text change and tell the fragment using onEditTextChanged object.

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        holder.anameTxtView.setText(mDataSet.get(position).getDname());
        holder.abalanceTxtView.setText(mDataSet.get(position).getDbalance());
    
        holder.adepositEditText.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) {
                onEditTextChanged.onTextChanged(position, charSequence.toString());
            }
    
            @Override
            public void afterTextChanged(Editable editable) {}
        });
    }
    

    Add this array to your GroupCollectionFragment so you can save the values in your fragment and use them whenever you want them.

    Integer[] enteredNumber = new Integer[1000];
    

    change your constructor call in GroupCollectionFragment

    mAdapter = new Adapter(holderList, new OnEditTextChanged() {
            @Override
            public void onTextChanged(int position, String charSeq) {
                enteredNumber[position] = Integer.valueOf(charSeq);
                updateTotalValue();
            }
        }); 
    
    private void updateTotalValue() {
        int sum = 0;
        for (int i = 0; i < 1000; i++) {
            sum += enteredNumber[i];
        }
    
        totalValue.setText(String.valueOf(sum));
    }
    

    Let me know if you want the whole files. I wrote it and built the apk, it works just fine.

提交回复
热议问题