Getting values from RecyclerView EditText?

前端 未结 5 1590
清酒与你
清酒与你 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:14

    I have modified @MeHdi's answer. There is little fault in @MeHdi's answer which will turn into a big issue, When there is a scroll up/down in recyclerview, items position get changed and its values are getting changed or becoming empty. Also I have number of editText in a single row of recyclerview. Thanks to @MeHdi's idea. Screen looks like below

    OnEditTextChanged

    public interface OnEditTextChanged {
    // here component_id is editTextId (findView by Id)
    void onTextChanged(int component_id, int position, String charSeq);
    }
    

    Declare below in adapter class globally

    private Context context;
    private List<DataModel> dailyLiqdateList;
    private OnEditTextChanged onEditTextChanged;
    

    Adapter class

    public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyHolder> {
    private Context context;
    private List<DataModel> dataList;
    
    private OnEditTextChanged onEditTextChanged;
    
    public MyAdapter(Context mContext, List<DataModel> dataList, OnEditTextChanged onEditTextChanged) {
        this.context = mContext;
        this.dataList = dataList;
        this.onEditTextChanged = onEditTextChanged;
    }
    
    @NonNull
    @Override
    public MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    
        View myHolder = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list,parent,false);
        return new MyHolder(myHolder);
    }
    @Override
    public int getItemCount() {
        return dataList.size();
    }
    
     // This is important method due to which even if you scroll list, editText values will not get changed/empty. This method will maintain its position in recyclerview
    @Override
    public int getItemViewType(int position) {
        return position;
    }
    
    @Override
    public void onBindViewHolder(MyHolder holder, int position) {
    
        holder.bind(dataList.get(position), position);
    
        holder.firstEditText.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) {
    
            }
    
            @Override
            public void afterTextChanged(Editable editable) {
                onEditTextChanged.onTextChanged(R.id.item_firstEditText,position, holder.firstEditText.getText().toString());
            }
        });
    
        holder.secondEditText.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) {
    
            }
    
            @Override
            public void afterTextChanged(Editable editable) {
                onEditTextChanged.onTextChanged(R.id.item_secondEditText, position, holder.secondEditText.getText().toString());
            }
        });
    
    
    }
    
    public class MyHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        TextView headerName;
        EditText firstEditText, secondEditText;
    
        MyHolder(View itemView) {
            super(itemView);
            headerName = itemView.findViewById(R.id.headerNameTv);
            firstEditText = itemView.findViewById(R.id.item_firstEditText);
            secondEditText = itemView.findViewById(R.id.item_secondEditText);;
    
        }
    
        public void bind(final DataModel dto, int position) {
            if (dto != null) {
                    headerName.setText(dto.getName());
            }
        }
    }
    }
    

    Your Activity class in which you have recyclerview

    // declare these values globally
    Integer[] firstValues, secondValues;
    // Declare textviews for calculating and setting total values
    private TextView firstTotalTv, secondTotalTv;
    
    // Initialize editText values array list, same as dataList size.
    firstValues= new Integer[dataList.size()];
    secondValues= new Integer[dataList.size()];
    
    // setting adapter on recyclerview, write this in onCreate()
    myAdapter = new MyAdapter(thisActivity, dataList, new OnEditTextChanged() {
            @Override
            public void onTextChanged(int component_id, int position, String charSeq) {
                if (component_id == R.id.item_firstEditText) {
                    if (Utils.isValidStr(charSeq)) {
                        firstValues[position] = Integer.valueOf(charSeq);
    
                    } else {
                        firstValues[position] = null;
                    }
                    updateTotalValue1(firstValues, firstTotalTv);
                }
    
                if (component_id == R.id.item_secondEditText) {
                    if (Utils.isValidStr(charSeq)) {
                        secondValues[position] = Integer.valueOf(charSeq);
                    } else {
                        secondValues[position] = null;
                    }
                    updateTotalValue1(secondValues, secondTotalTv);
                }                
            }
        });
        recyclerView.setAdapter(myAdapter);
    
    
       // Update Method, write outside of onCreate()
       private void updateTotalValue1(Integer[] editTextList, TextView totalTextView) {
        int pSum = 0, c1Sum = 0;
        for (int i = 0; i < editTextList.length; i++) {
            if (editTextList[i] != null)
                pSum += editTextList[i];
            else if (i == 0 && editTextList[i] == null)
                pSum = 0;
        }
        if (pSum > 0)
            totalTextView.setText(String.valueOf(pSum));
        else
            totalTextView.setText("");
    }
    
    0 讨论(0)
  • 2021-01-05 13:15

    Use TextChangedListener on EditText and save input in new HashMap with key as a id of order/unique key for row.

       adeposit.addTextChangedListener(new TextWatcher() {
    
       public void afterTextChanged(Editable s) {}
    
      public void beforeTextChanged(CharSequence s, int start,
                                    int count, int after) {
      }
    
     public void onTextChanged(CharSequence s, int start,
                               int before, int count) {
      // Save value her in HashMap
      }
    });
    

    At the end get Values from HashMap.

    0 讨论(0)
  • 2021-01-05 13:16

    You can get value at action done of keyboard. all you need is to set

    android:imeOptions="actionDone" in edit text. and then just use below code

     adepositEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
                    @Override
                    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                        if (actionId == EditorInfo.IME_ACTION_DONE) {
                            // Do whatever you want here
    
                            return true;
                        }
                        return false;
    
                    });
    
    0 讨论(0)
  • 2021-01-05 13:19

    A better approach to performance

    private List<Integer> getQuantityList() {
        List<Integer> quantities = new ArrayList<>();
    
        for (int i = 0; i < cartItems_rv.getChildCount(); i++) {
            quantities.add(Integer.valueOf(((EditText)Objects.requireNonNull(
                    Objects.requireNonNull(cartItems_rv.getLayoutManager()).findViewByPosition(i))
                    .findViewById(R.id.quantity_et)).getText().toString()));
        }
    
        return quantities;
    }
    

    thanks for Goku's answers, feel free to shape the method for your own app.

    0 讨论(0)
  • 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<DataHolder> mDataSet;
    private OnEditTextChanged onEditTextChanged;
    
    public Adapter(List<DataHolder> 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.

    0 讨论(0)
提交回复
热议问题