I am struck in recyclerView,
Here the name and balance fields are coming from two different arrays. What I need is, here each row has an EditText field. I need to access each EditText on each row. And get values from it.. and a total is displayed on the Total textView. Is it possible? I tried a lot. I didn't get it.
i am attaching my classes here.
MainActivity
public class GroupCollectionFragment extends Fragment {
String[] nameArray = {"Akhil","Mohan","Anoop","Syam","Athul","Anish","Anand","Prasad","Mani","Oommen"
,"Akhil","Mohan","Anoop","Syam","Athul","Anish","Anand","Prasad","Mani","Oommen"
,"Akhil","Mohan","Anoop","Syam","Athul","Anish","Anand","Prasad","Mani","Oommen"};
String[] balanceArray={"2354","6578","2345","34654","2542","2354","6578","2345","34654","2542"
,"2354","6578","2345","34654","2542","2354","6578","2345","34654","2542"
,"2354","6578","2345","34654","2542","2354","6578","2345","34654","2542"};
RecyclerView mRecyclerView;
RecyclerView.Adapter mAdapter;
LinearLayoutManager mLayoutManager;
List<DataHolder> holderList=new ArrayList<DataHolder>();
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootview=inflater.inflate(R.layout.group_collection_layout,container,false);
mRecyclerView = (RecyclerView) rootview.findViewById(R.id.my_recycler_view);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(getContext());
mRecyclerView.setLayoutManager(mLayoutManager);
setItems();
mAdapter = new Adapter(holderList);
mRecyclerView.setAdapter(mAdapter);
return rootview;
}
private void setItems() {
for(int i=0;i<nameArray.length;i++){
DataHolder item=new DataHolder();
item.setDname(nameArray[i]);
item.setDbalance(balanceArray[i]);
holderList.add(item);
}
}
}
DataHolder
public class DataHolder {
String dname,dbalance;
public DataHolder(){
}
public String getDname(){
return dname;
}
public void setDname(String name){
this.dname=name;
}
public String getDbalance(){
return dbalance;
}
public void setDbalance(String balance){
this.dbalance=balance;
}
}
Adapter
public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder> {
private List<DataHolder> mDataSet;
public static class ViewHolder extends RecyclerView.ViewHolder{
private TextView anameTxtView,abalanceTxtView;
private EditText adepositEditText;
public ViewHolder(View v){
super(v);
anameTxtView=(TextView)v.findViewById(R.id.nameTextView);
abalanceTxtView=(TextView)v.findViewById(R.id.balanceTextView);
adepositEditText=(EditText)v.findViewById(R.id.depositEditText);
}
}
public Adapter(List<DataHolder> myData){
mDataSet=myData;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v=LayoutInflater.from(parent.getContext()).inflate(R.layout.row_main,parent,false);
ViewHolder vh=new ViewHolder(v);
return vh;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.anameTxtView.setText(mDataSet.get(position).getDname());
holder.abalanceTxtView.setText(mDataSet.get(position).getDbalance());
}
@Override
public int getItemCount() {
return mDataSet.size();
}
@Override
public int getItemViewType(int position) {
return position;
}
}
I think you are looking for a callback, which means whenever a number on one of the EditTexts is changed you want the total number change too. So first of all you need to add an interface,
OnEditTextChanged Interface
public interface OnEditTextChanged {
void onTextChanged(int position, String charSeq);
}
Then you need too include this in the constructor of the adapter.
In the Adapter.java
private List<DataHolder> mDataSet;
private OnEditTextChanged onEditTextChanged;
public Adapter(List<DataHolder> myData, OnEditTextChanged onEditTextChanged) {
mDataSet = myData;
this.onEditTextChanged = onEditTextChanged;
}
In the onBindViewHolder of your Adapter you need to set a listener for text change and tell the fragment using onEditTextChanged object.
@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
holder.anameTxtView.setText(mDataSet.get(position).getDname());
holder.abalanceTxtView.setText(mDataSet.get(position).getDbalance());
holder.adepositEditText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
onEditTextChanged.onTextChanged(position, charSequence.toString());
}
@Override
public void afterTextChanged(Editable editable) {}
});
}
Add this array to your GroupCollectionFragment so you can save the values in your fragment and use them whenever you want them.
Integer[] enteredNumber = new Integer[1000];
change your constructor call in GroupCollectionFragment
mAdapter = new Adapter(holderList, new OnEditTextChanged() {
@Override
public void onTextChanged(int position, String charSeq) {
enteredNumber[position] = Integer.valueOf(charSeq);
updateTotalValue();
}
});
private void updateTotalValue() {
int sum = 0;
for (int i = 0; i < 1000; i++) {
sum += enteredNumber[i];
}
totalValue.setText(String.valueOf(sum));
}
Let me know if you want the whole files. I wrote it and built the apk, it works just fine.
A better approach to performance
private List<Integer> getQuantityList() {
List<Integer> quantities = new ArrayList<>();
for (int i = 0; i < cartItems_rv.getChildCount(); i++) {
quantities.add(Integer.valueOf(((EditText)Objects.requireNonNull(
Objects.requireNonNull(cartItems_rv.getLayoutManager()).findViewByPosition(i))
.findViewById(R.id.quantity_et)).getText().toString()));
}
return quantities;
}
thanks for Goku's answers, feel free to shape the method for your own app.
You can get value at action done of keyboard. all you need is to set
android:imeOptions="actionDone"
in edit text. and then just use below code
adepositEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
// Do whatever you want here
return true;
}
return false;
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With