我创建了适配器来存储带有动态数量的购物车商品。
添加包含尺寸,打印数量,价格的自定义视图。在那我必须采取否。来自用户输入的 EditText 中的“打印”。
适配器的 onBindViewHolder()
:我为每个 EditText 添加了常见的TextWatcher
,其中 ID 传入构造函数。
/*
* Add Custom View For Size and Price
* */
if (cart.getCartPhotoPrintSize() != null) {
try {
data = cart.getCartPhotoPrintSize().get(position);
if (data != null) {
// Remove the custom view
holder.cartPhotoSizrPriceCustome.removeAllViews();
holder.cartPhotoSizrPriceCustome.setVisibility(View.VISIBLE);
// Local Variable for Storing the size of StudioData Size
int size = cart.getCartPhotoPrintSize().size();
// Loop for set the custom layout
for (int i = 0; i < size; i++) {
// inner Loacl Variable
Studio.StudioSize studioSize = cart.getCartPhotoPrintSize().get(i);
// Layout Inflater For Targeting The RootView Of Context
LayoutInflater inflater = LayoutInflater.from(mContext);
View sizePriceView = inflater.inflate(R.layout.size_price_quantity_layout, null, false);
// Binding the id of TextView
studioSizeTextView = (TextView) sizePriceView.findViewById(R.id.studio_photo_album_size);
studioPriceTextView = (TextView) sizePriceView.findViewById(R.id.studio_photo_album_price);
photoQuantity = (EditText) sizePriceView.findViewById(R.id.photo_quantity);
studioSizeTextView.setId(i);
studioSizeTextView.setText(studioSize.getSize());
studioPriceTextView.setText("" + studioSize.getPrice());
holder.cartPhotoSizrPriceCustome.setTag(cart.getId());
holder.cartPhotoSizrPriceCustome.addView(sizePriceView);
//Add TextWacher For Every EditTextView
photoQuantity.addTextChangedListener(new GeneralTextWatcher(holder, i));
}
}
} catch (Exception e) {
Log.w("TAG", e.getMessage());
}
}
private class GeneralTextWatcher implements TextWatcher {
ViewHolder viewHolder;
int id;
public GeneralTextWatcher(ViewHolder viewHolder, int i) {
this.viewHolder = viewHolder;
this.id = i;
Log.e(TAG, "Constructor Calling..");
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
totalAmount = 0;
int child = viewHolder.cartPhotoSizrPriceCustome.getChildCount();
int pos = viewHolder.getAdapterPosition();
Log.i("size position : ", "" + id);
Log.i("cart item position : ", "" + pos);
Cart tCart = tempValue.get(pos);
Studio.StudioSize ssTemp = tCart.getCartPhotoPrintSize().get(id);
ssTemp.setQuantity(!TextUtils.isEmpty(s) ? Integer.parseInt(s.toString()) : 0);
for (int j = 0; j < child; j++) {
View view = viewHolder.cartPhotoSizrPriceCustome.getChildAt(j);
EditText editText = (EditText) view.findViewById(R.id.photo_quantity);
TextView textView = (TextView) view.findViewById(R.id.studio_photo_album_price);
Double quantity = 0.0;
if (!editText.getText().toString().equals(""))
quantity = Double.parseDouble(editText.getText().toString());
Double price = Double.parseDouble(textView.getText().toString());
if (printAmount != 0)
tempTotal = printAmount;
printAmount = quantity * price;
totalAmount += printAmount;
}
tempValue.get(pos).setTotal(totalAmount);
((CartActivity) mContext).mValues = tempValue;
Log.e("Tag", "After tmpStr " + new Gson().toJson(
tempValue,
new TypeToken<ArrayList<Cart>>() {
}.getType()));
((CartActivity) mContext).findTotalAmount(tempValue);
}
@Override
public void afterTextChanged(Editable s) {
}
}
EditText
值并保存新的JSON就像Click to see JSON after 3rd EditText filled up其工作正常,但是
EditText
值并保存JSON,如Click to see after changing 4th EditText in Second Cart更换旧值。自从过去两天以来,我遇到了这个问题。任何帮助,将不胜感激。
谢谢。