Изображение
У меня есть RecyclerView
, в котором поля создаются динамически. Чтобы сохранить сохраненные данные, я использовал TextWatcher. Но я не знаю, как я суммирую значения, которые находятся в EditText -Cost, и отображаю значение в TextView внизу экрана
public class ItemsAdapter extends
ListAdapter<Item,ItemsAdapter.ViewHolder> {
private List<Item> mItems=new ArrayList<>();
public static final DiffUtil.ItemCallback<Item>DIFF_CALLBACK=
new DiffUtil.ItemCallback<Item>() {
@Override
public boolean areItemsTheSame( Item oldItem, Item newItem) {
return oldItem.getId()==newItem.getId();
}
@Override
public boolean areContentsTheSame(Item oldItem, Item newItem) {
return(oldItem.getName()==newItem.getName()&&oldItem.getCost()==newItem.getCost());
}
};
public ItemsAdapter(){super(DIFF_CALLBACK);}
public void addMorreItems(List<Item> newItems){
int insertionPosition=mItems.size();
mItems.addAll(newItems);
notifyItemRangeInserted(insertionPosition,newItems.size());
submitList(mItems);
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
Context context=parent.getContext();
LayoutInflater inflater=LayoutInflater.from(context);
View itemView=inflater.inflate(R.layout.custom_edittext_layout,parent,false);
ViewHolder viewHolder=new ViewHolder(itemView);
return viewHolder;
}
@Override
public void onBindViewHolder(ViewHolder viewHolder,int position){
viewHolder.editTextName.setText(mItems.get(position).getName());
viewHolder.editTextCost3.setText(mItems.get(position).getCost());
Item item=getItem(position);
EditText editText=viewHolder.editTextName;
editText.setText(item.getName());
EditText editText1=viewHolder.editTextCost3;
editText1.setText(item.getCost());
}
public class ViewHolder extends RecyclerView.ViewHolder {
public EditText editTextName;
public EditText editTextCost3;
public TextView txtTipAmount3;
public Button buttonDelete;
public Button buttonOK;
public ViewHolder(View itemView){
super(itemView);
editTextCost3=(EditText)itemView.findViewById(R.id.ediCost3);
editTextName=(EditText)itemView.findViewById(R.id.editTextEnterYourName);
txtTipAmount3=(TextView)itemView.findViewById(R.id.txtTipAmount3);
buttonDelete=(Button)itemView.findViewById(R.id.buttonDelete);
buttonOK=(Button)itemView.findViewById(R.id.buttonOk);
buttonDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int position=getAdapterPosition();
try {
mItems.remove(position);
notifyItemRemoved(position);
}catch (ArrayIndexOutOfBoundsException e){e.printStackTrace();}
}
});
editTextName.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
mItems.get(getAdapterPosition()).setmName(editTextName.getText().toString());
}
@Override
public void afterTextChanged(Editable s) {
}
});
editTextCost3.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
mItems.get(getAdapterPosition()).setmCost(editTextCost3.getText().toString());
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
} }