Reputation: 199
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());
}
});
}
Upvotes: 0
Views: 726
Reputation: 3607
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.
Upvotes: 1
Reputation: 191728
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
Upvotes: 1