Search code examples
androidandroid-edittextandroid-recyclerviewandroid-viewholder

Multiple edittext in Recyclerview


We're making an app which makes it possible to share group costs. In order to do this you can select people with whom you would like to share the next bill. I made a sort of "list-view-item" which contains a "Debit" and "Credit" field for each user and added these items to a Recyclerview according to how many people are sharing the payment. However, now i am trying to read all the values that were entered in these edittexts but i don't know how. The code that generates the the list in the recyclerview is the following:

private void initRecyclerView() {
    rvTransactions = (RecyclerView) findViewById(R.id.rvTransactions);
    lmTransactions = new LinearLayoutManager(this);
    rvTransactions.setLayoutManager(lmTransactions);
    transactionAdapter = new RecyclerView.Adapter<CustomViewHolder>() {
        @Override
        public CustomViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_item_transaction, viewGroup, false);
            return new CustomViewHolder(view);
        }

        @Override
        public void onBindViewHolder(CustomViewHolder viewHolder, int i) {
            viewHolder.noticeSubject.setText(participatingUsers.get(i).getFirstname() + " " + participatingUsers.get(i).getLastname());
        }

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

    };
    rvTransactions.setAdapter(transactionAdapter);
}

private class CustomViewHolder extends RecyclerView.ViewHolder {

    private TextView noticeSubject;
    private EditText userDebt,userCredit;

    public CustomViewHolder(View itemView) {
        super(itemView);

        noticeSubject = (TextView) itemView.findViewById(R.id.tvTransactionUser);
        userCredit = (EditText) findViewById(R.id.etTransactionPayed);
        userDebt = (EditText) findViewById(R.id.etTransactionOwes);
    }
}

The participatingUsers object is a list of users. A user has the following fields:

private int id;
private boolean isSelected;
private String firstname, lastname, email, password, api_key, creditcardnumber;

This is what the view looks like, you can see the "payed" and "owes" fields that were added dynamicly

I want to be able to get the values that were entered in the edittexts "payed" and "owes" for all the users.


Solution

  • Loop over the childCount of the RecyclerView and check to make sure the item your reading is of type CustomViewHolder, cast to that type and read EditText Values. Something like:

     public void printAllEditTextValues(){
         int childCount = rvTransactions.getChildCount(); 
         for(int i = 0; i < childCount; i++){
             if(rvTransactions.findViewHolderForLayoutPosition(i) instanceOf CustomViewHolder){
               CustomViewHolder childHolder = (CustomViewHolder) rvTransactions.findViewHolderForLayoutPosition(i);
    
               Log.d("EDIT_TEXT_1" , childHolder.userCredit.getText().toString());
               Log.d("EDIT_TEXT_2" , childHolder.userDebt.getText().toString());
             }
         }
    

    your adapter is missing thie itemView.findViewById(int) for your edit text, use:

    private class CustomViewHolder extends RecyclerView.ViewHolder {
    
        private TextView noticeSubject;
        private EditText userDebt,userCredit;
    
        public CustomViewHolder(View itemView) {
            super(itemView);
    
            noticeSubject = (TextView) itemView.findViewById(R.id.tvTransactionUser);
            userCredit = (EditText) itemView.findViewById(R.id.etTransactionPayed);
            userDebt = (EditText) itemView.findViewById(R.id.etTransactionOwes);
        }
    }