I have recyclerview with edit text. Each row has a edit text. I am entering values in the edit text manually and after i enter values, I want to get those values in each and eve
I had a similar problem.My Recyclerview contained one Textview, two EditTexts and one remove Button to remove the item from the Recyclerview. I am getting the data from both the Edittexts, from a model class using a button from my activity. Everything works well I just want to know what all optimizations I can make. Please give your suggestions in a detailed manner.
My Model Class:
public class OrderGS {
String names, rty,qty;
public OrderGS(String name, String rty, String qty) {
this.names = name;
this.rty = rty;
this.qty = qty;
}
public String getName() {
return names;
}
public void setName(String names) {
this.names = names;
}
public String getRty() {
return rty;
}
public void setRty(String rty) {
this.rty = rty;
}
public String getQty() {
return qty;
}
public void setQty(String qty) {
this.qty = qty;
}
}
Above class holds the three Strings one is the titile and the two hold the edittext data.
My Adapter code:
class OOHolderI extends RecyclerView.ViewHolder{
TextView title;
EditText rty,qty;
public OOHolderI(View itemView) {
super(itemView);
title = itemView.findViewById(R.id.text_order);
rty = itemView.findViewById(R.id.editRTY);
qty = itemView.findViewById(R.id.editQTY);
itemView.findViewById(R.id.button_remove).setOnClickListener(v -> {
arrayList.remove(getAdapterPosition());
if(rtyArray.size()>0) {
rtyArray.remove(getAdapterPosition());
qtyArray.remove(getAdapterPosition());
}
notifyDataSetChanged();
});
rty.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) {
}
@Override
public void afterTextChanged(Editable s) {
arrayList.get(getAdapterPosition()).setRty(s.toString());
if(!rtyArray.contains(getAdapterPosition())){
rtyArray.add(getAdapterPosition());}
}
});
qty.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) {
}
@Override
public void afterTextChanged(Editable s) {
arrayList.get(getAdapterPosition()).setQty(s.toString());
if(!qtyArray.contains(getAdapterPosition())) {
qtyArray.add(getAdapterPosition());
}
}
});
}
}
In the adapter I am using two arraylists rtyArray
and qtyArray
to hold the position of the Edittext data in the model class.
public ArrayList getRTY() {
return rtyArray;
}
public ArrayList getQTY(){
return qtyArray;
}
The above two functions return the positions of the Edittextdata
My Activity:
findViewById(R.id.button_save).setOnClickListener(v -> {
ArrayList rtyArray = adapter.getRTY();
ArrayList qtyArray = adapter.getQTY();
if(rtyArray.size()>0) {
for(Integer i: rtyArray){
//get data here like
arrayListOfModel.get(i).getRty();
arrayListOfModel.get(qtyArray.get(i)).getQty();
}
}
});
adapter.notifyDataSetChanged();
In my case both the position holding arrays are always equal in size.