Common TextWatcher Saving Values in JSON Improperly (Replacing old values)
<p>I have created <strong>Adapter</strong> to store Cart Items with there <strong>dynamic</strong> quantities.</p>

<p>Adding custom views which Contains Size, No. of Print, Price. In that I have to take <strong>No. of Print</strong> in <strong>EditText</strong> from User Input.</p>

<p><a href="https://i.stack.imgur.com/mmk2fl.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/mmk2fl.png" alt="Cart"></a></p>

<p><code>onBindViewHolder()</code> of <strong>Adapter</strong>: In that I have added common <code>TextWatcher</code> for every <strong>EditText</strong> which is separated by <strong>ID</strong> passed in Constructor.</p>

<pre><code>   /*
    *   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 &lt; 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());
        }
    }
</code></pre>

<h2>GeneralTextWatcher</h2>

<pre><code>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 &lt; 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&lt;ArrayList&lt;Cart&gt;&gt;() {
                }.getType()));
        ((CartActivity) mContext).findTotalAmount(tempValue);
    }
    @Override
    public void afterTextChanged(Editable s) {
    }
}
</code></pre>

<ul>
<li>When I Change <strong>First Cart</strong> Item with all three <code>EditText</code> values and Saving new JSON is like <a href="http://pastebin.com/zUVy7mMe" rel="nofollow noreferrer">Click to see JSON after 3rd EditText filled up</a> Its Working Fine,</li>
</ul>

<p>But</p>

<ul>
<li>After changing <strong>Second Cart</strong> Item fourth <code>EditText</code> Value and Saving JSON like as <a href="http://pastebin.com/UYwiWbrf" rel="nofollow noreferrer">Click to see after changing 4th EditText in Second Cart</a> Its replacing old Values.</li>
</ul>

<p>I am getting this problem since last two days. Any help would be appreciated. </p>

<p>Thank you.</p>