Reputation: 1924
I have called recyclerview in which my arraylist is attached with the adapter. And when adapter has its own row file in which I have taken one edit text and manually I can enter the amount.
But the issue is when I scroll the recyclerview, edit text value has gone. So can't see the values whichever entered by me.
Adapter Class
public class AddSecondarySalesOrderProductDetailsAdapter extends RecyclerView.Adapter {
ArrayList<SecondarySalesProductDetailsModel> mArrSecondarySalesProductDetailsModel;
ArrayList<String> mArrProductQuantity;
private int mViewItem = 1;
private Activity mActivity;
private LayoutInflater mLayoutInflater;
ImageView mImageDeleteProduct;
private String mProductName, mProductAvaQuantity, mProductSKU, mOrderedQuantity;
/**
* Adapter contains the data to be displayed
*/
public AddSecondarySalesOrderProductDetailsAdapter(Activity mActivity, ArrayList<SecondarySalesProductDetailsModel>
mArrSecondarySalesProductDetailsModel) {
this.mActivity = mActivity;
this.mArrSecondarySalesProductDetailsModel = mArrSecondarySalesProductDetailsModel;
mArrProductQuantity = new ArrayList<>();
mLayoutInflater = LayoutInflater.from(mActivity);
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = mLayoutInflater.inflate(R.layout.raw_add_secondary_sales_order_product_details, parent,
false);
return new CustomViewHolder(view);
}
@Override
public int getItemViewType(int position) {
return mArrSecondarySalesProductDetailsModel.get(position) != null ? mViewItem : 0;
}
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
holder.setIsRecyclable(false);
final SecondarySalesProductDetailsModel mAllProductDetailsModel = mArrSecondarySalesProductDetailsModel.get(position);
((CustomViewHolder) holder).mImageDeleteProduct.setTag(position);
mProductName = mAllProductDetailsModel.getProductName();
mProductAvaQuantity = String.valueOf(mAllProductDetailsModel.getAvalQty());
mProductSKU = mAllProductDetailsModel.getSku();
((CustomViewHolder) holder).mTextProductName.setText(mProductName);
((CustomViewHolder) holder).mTextProductAvaQuantity.setText(mProductAvaQuantity);
((CustomViewHolder) holder).mTextProductSKU.setText(mProductSKU);
((CustomViewHolder) holder).mImageDeleteProduct.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mArrSecondarySalesProductDetailsModel.remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position, mArrSecondarySalesProductDetailsModel.size());
if (mArrSecondarySalesProductDetailsModel.size() == 0) {
AddSecondarySalesOrderProductDetailFragment.mRelativeNoRecords.setVisibility(View.VISIBLE);
}
}
});
OrderedQuantityTextWatcher textWatcher = new OrderedQuantityTextWatcher(holder, ((CustomViewHolder) holder).mEditOrderedQuantity, position);
((CustomViewHolder) holder).mEditOrderedQuantity.addTextChangedListener(textWatcher);
((CustomViewHolder) holder).mEditOrderedQuantity.setTag(position);
}
private class OrderedQuantityTextWatcher implements TextWatcher {
private EditText editText;
private int position;
private RecyclerView.ViewHolder holder;
public OrderedQuantityTextWatcher(RecyclerView.ViewHolder holder, EditText editText, int
pos) {
this.holder = holder;
this.editText = editText;
this.position = pos;
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// mOrderedQuantity = mEditOrderedQuantity.getText().toString().trim();
}
@Override
public void afterTextChanged(Editable editable) {
// int position = holder.getAdapterPosition();
int position = (int) editText.getTag();
Common.insertLog("Position: " + position);
Common.insertLog("Value: " + editable);
SecondarySalesProductDetailsModel secondarySalesProductDetailsModel = new
SecondarySalesProductDetailsModel();
secondarySalesProductDetailsModel.setId(mArrSecondarySalesProductDetailsModel.get(position).getId());
secondarySalesProductDetailsModel.setProductName
(mArrSecondarySalesProductDetailsModel.get(position).getProductName());
secondarySalesProductDetailsModel.setAvalQty(mArrSecondarySalesProductDetailsModel
.get(position).getAvalQty());
secondarySalesProductDetailsModel.setOrderedQty(editable.toString().trim());
secondarySalesProductDetailsModel.setSku(mArrSecondarySalesProductDetailsModel.get
(position).getSku());
secondarySalesProductDetailsModel.setProductId(mArrSecondarySalesProductDetailsModel
.get(position).getProductId());
secondarySalesProductDetailsModel.setSelected(mArrSecondarySalesProductDetailsModel
.get(position).getSelected());
// mArrSecondarySalesProductDetailsModel.add(secondarySalesProductDetailsModel);
Intent intent = new Intent(AppConstants.BROADCAST_SEND_SECONDARY_PRODUCTS);
intent.putExtra(AppConstants.BUNDLE_BROADCAST_ORDERED_QUANTITY,
secondarySalesProductDetailsModel);
LocalBroadcastManager.getInstance(mActivity).sendBroadcast(intent);
}
}
@Override
public int getItemCount() {
return mArrSecondarySalesProductDetailsModel.size();
}
/**
* Initialization of components
*/
public class CustomViewHolder extends RecyclerView.ViewHolder {
TextView mTextProductName, mTextProductAvaQuantity, mTextProductSKU;
EditText mEditOrderedQuantity;
ImageView mImageDeleteProduct;
public CustomViewHolder(final View itemView) {
super(itemView);
this.mImageDeleteProduct = (ImageView) itemView.findViewById(R.id
.image_raw_secondary_sales_order_delete);
this.mTextProductName = (TextView) itemView.findViewById(R.id.text_raw_secondary_sales_order_product_name);
this.mTextProductAvaQuantity = (TextView) itemView.findViewById(R.id
.text_raw_secondary_sales_order_ava_quantity);
this.mTextProductSKU = (TextView) itemView.findViewById(R.id
.text_raw_secondary_sales_order_product_sku);
this.mEditOrderedQuantity = (EditText) itemView.findViewById(R.id
.edit_raw_secondary_sales_order_quantity);
}
}
Upvotes: 0
Views: 1713
Reputation: 97
Fo those still looking for the solution, here is solution for them
steps :
create a hashmap something like ths - HashMap<Integer, Network> editedMapList = new HashMap<Integer, Network>();
In onBind method based on textchanged add the changed item in the hashmap based on position
When when scroll down row will recreate for that u have set value again something like this in onBind method of adapter. //For recycled item update if (editedMapList.containsKey(position)) { ((NetworkItemViewHolder) holder).etUsername.setText(editedMapList.get(position).getUsername()); }
Note: Here the logic is when u scroll down row will recreate that means onBind method will call, so wat u have to do is update the row edittext based on position from the editted hashmap.
Hope it helps ! Happy coding...
Upvotes: 2
Reputation: 52
Problem : if Your edittext value is 0 on Scrolling the recyclerview
Solution : then Use below Code to your Edittext inside textwatcher method afterTextchange()
Code : yourEditTextName.setSelection(yourEditTextName.getText().length());
Upvotes: 0
Reputation: 301
i model class create one more field and set as empty on object creation , set to edit text in onBindViewHolder() , on text change update that object too..
Upvotes: 0