0

I want to send text value of EditText from RecyclerView Adapter to an Activity. In my scenario, I need to send EditText text from Adapter to an Activity when a button pressed (The button is in Activity class not in Adapter). In this code of Adapter class,

viewHolder.value.setText(memberUpdate.getAmount());

I've set value of EditText. Now i want to get this value from this adapter to Activity class whenever Button clicked. The Button which going to be clicked is updateBtn = findViewById(R.id.update_expense_btn); in Activity class. kindly tell me how i get values of EditText which is of Adapter class to Activity class.

Adapter Class:

   class AdapterUpdateExpense extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private final int MEMBERUPDATE = 4;
    private List<Object> items;
    private Context context;
    private Activity activity;
    private boolean isEvenlyRadioChecked;
    private Double amount = 0.0;

    AdapterUpdateExpense(Activity activity, Context context, List<Object> items, boolean isEvenlyRadioChecked) {
        this.activity = activity;
        this.context = context;
        this.items = items;
        this.isEvenlyRadioChecked = isEvenlyRadioChecked;
    }

    @Override
    public int getItemViewType(int position) {
        if (items.get(position) instanceof MemberUpdate) {
            return MEMBERUPDATE;
        }
        return -1;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        RecyclerView.ViewHolder holder = null;
        LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());

        switch (viewType) {
            case MEMBERUPDATE:
                View v4 = inflater.inflate(R.layout.update_expense_layout, viewGroup, false);
                holder = new holderMemberUpdate(v4);
                break;
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
        switch (viewHolder.getItemViewType()) {
            case MEMBERUPDATE:
                holderMemberUpdate view4 = (holderMemberUpdate) viewHolder;
                configureHolderMemberUpdate(view4, position);
                break;
        }
    }

    private void configureHolderMemberUpdate(final  holderMemberUpdate viewHolder, final int position) {
        final MemberUpdate memberUpdate = (MemberUpdate) items.get(position);

        if (memberUpdate != null) {
            viewHolder.name.setText(memberUpdate.getTitle());
            viewHolder.value.setText(memberUpdate.getAmount());
        }

        amount+= Double.valueOf(viewHolder.value.getText().toString());
        Log.d("sdfsdfah", getAmount().toString());
    }

    class holderMemberUpdate extends RecyclerView.ViewHolder{
        TextView name;
        EditText value;

        holderMemberUpdate(View view) {
            super(view);
            name = view.findViewById(R.id.member_name_txtview);
            value = view.findViewById(R.id.member_expense_edittxt);

            if(isEvenlyRadioChecked) {
                value.setEnabled(false);
            } else {
                value.setEnabled(true);
                value.setTextColor(context.getResources().getColor(R.color.colorAccent));
            }

        }
    }

Activity Class:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update_expense);

        Intent intent = getIntent();
        if (intent.getExtras() != null) {
            receipt = intent.getStringExtra("receipt");
            Log.d("receiptIntent", receipt);
        }


        getReference();


    }

    void getReference() {
updateBtn = findViewById(R.id.update_expense_btn);
        updateBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                memberAmount = ((AdapterUpdateExpense)recyclerView.getAdapter()).getAmount();
                Log.d("memberAmount", memberAmount.toString());
            }
        });
    }
Amin Memariani
  • 830
  • 3
  • 17
  • 39
Zohaib
  • 189
  • 1
  • 5
  • 16
  • 1
    Possible duplicate of [How pass data from RecyclerView to activity](https://stackoverflow.com/questions/47182944/how-pass-data-from-recyclerview-to-activity) – Aditya Feb 10 '18 at 15:39
  • no, in my scenario, the button which going to pressed is in Activity class not in adapter class – Zohaib Feb 10 '18 at 15:43
  • Do you want to pass data from activity to adapter ? – Aditya Feb 10 '18 at 15:44
  • Adapter to activity – Zohaib Feb 10 '18 at 15:44
  • EditText which is in Adapter class need to send there text from adapter to activity when an activity's button clicked. – Zohaib Feb 10 '18 at 15:45
  • Hopefully you got my point – Zohaib Feb 10 '18 at 15:45
  • 1
    When you scroll the recycler, you will lose the value entered in edit text until it is not stored in adapter data. Always prefer to store it the in datasets of adapter. Once user tap on buttin in activity , process the dataset of adapter. – lib4backer Feb 10 '18 at 15:54
  • i think your idea is best could you please refer any live example ? @lib4 – Zohaib Feb 10 '18 at 16:04
  • You already have a `List`... You should 1) not make those plain objects 2) define a getter method so you may access its data outside of the adapter – OneCricketeer Feb 10 '18 at 16:17

2 Answers2

1

You better implement TextChangeListener, so whenever a user edits the value in edit text, you can store the newly edited value in a new variable in List items or a separate hashamp to store.

//Hashmap to store edited amount. Is static to access this in Activity class.
    public static HashMap<Integer,String> newAmountHashMap =  new HashMap<Integer, String>();


class holderMemberUpdate extends RecyclerView.ViewHolder{
        TextView name;
        EditText value;

        holderMemberUpdate(View view) {
            super(view);
            name = view.findViewById(R.id.member_name_txtview);
            value = view.findViewById(R.id.member_expense_edittxt);
value.addTextChangedListener(new MyCustomEditTextListener(this));
            if(isEvenlyRadioChecked) {
                value.setEnabled(false);
            } else {
                value.setEnabled(true);
                value.setTextColor(context.getResources().getColor(R.color.colorAccent));
            }

        }
    }

//Text change Listener

private class MyCustomEditTextListener implements TextWatcher {
        private int position;

        public void updatePosition(int position) {
            this.position = position;
        }

        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
            // no op
        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {

        }

        @Override
        public void afterTextChanged(Editable editable) {
            newAmountHashMap.put(position, editable.toString());
        }
    }

And Finally, you should process the HashMap in button tap in Activity.

lib4backer
  • 3,337
  • 3
  • 18
  • 16
  • Your solution will be best for one EditText but in Recyclerview, there are multiple EditText then what will do for remaining EditText? – Zohaib Feb 10 '18 at 16:33
  • 1
    This work for multiple edit text box. not for one. The whole point of doing this is to support multiple edit text. – lib4backer Feb 10 '18 at 16:34
  • You added a `this` parameter without having a constructor and updatePosition is never used – OneCricketeer Feb 10 '18 at 16:34
  • @cricket_007 minor modification should be done by the questioner, as this is to show him the path how to do it. expected minor logical modification. – lib4backer Feb 10 '18 at 16:36
  • Sure, but passing static non final variables around in Android is often seen as a poor design – OneCricketeer Feb 10 '18 at 16:38
  • right...I am little confuse, how can we get of each EditText Values individually? kindly tell me also @lib4 – Zohaib Feb 10 '18 at 16:38
  • @lib4 i think i got point ewAmountHashMap.put(position,charSequence.toString()); by this this problem can resolve. – Zohaib Feb 10 '18 at 16:40
  • Awesome. Glad It helped. Please do upvote as well to help others. @Zohaib – lib4backer Feb 10 '18 at 16:46
  • @cricket_007 i'll not use static array. i've another solution make function in adapter public HashMap getMemberAmountList() { return memberAmountList; } which return array and in activity on button click i'll get memberAmountList = ((AdapterUpdateExpense)recyclerView.getAdapter()).getMemberAmountList(); by this manner. – Zohaib Feb 11 '18 at 09:25
  • The hashmap is static, when it doesn't need to be @Zohaib – OneCricketeer Feb 11 '18 at 18:41
1

You need a TextWatcher on the value EditTexts that will set the value of the objects within List<Object> items, which should really not just be Object type, but at least some abstract Member type with a setAmount method.

private void configureHolderMemberUpdate(final  holderMemberUpdate viewHolder, final int position) {
    final MemberUpdate memberUpdate = (MemberUpdate) items.get(position);

    if (memberUpdate != null) {
        viewHolder.name.setText(memberUpdate.getTitle());
        viewHolder.value.setText(memberUpdate.getAmount());
    }

    // Add this
    viewHolder.value.addTextChangedListener(... 

Within the text watcher,

items.get(position).setAmount(Double.parseDouble(text));

How to use the TextWatcher class in Android?

Then, write a method to loop over items and sum those values, without any references to any views, or parsing anything.

That method can be written in the adapter, or the activity, where you passed in the list object to the activity

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • Your solution will be best for one EditText but in Recyclerview, there are multiple EditText then what will do for remaining EditText? – Zohaib Feb 10 '18 at 16:33
  • Every text box has its own text watcher and it controls the corresponding list element at that position – OneCricketeer Feb 10 '18 at 16:35