title: Variable value resets in onBindViewHolder as i am scrolling down my recycler view for the first time
body: <p><strong>Basic Idea of What I am doing:</strong></p>
  
  <p>I am setting up my recycler view from within my 'Categories' Fragment, which displays a product image, its title, its price and a counter, which when clicked changes the quantity of that particular item. I have a global variable called totalQTY defined in my ProductRecyclerViewAdapter which stores the total quantity of all the products in the generated recycler view and sends back the data to a method inside my 'Categories' Fragment.</p>
  
  <p><strong>What is the problem i am facing?</strong></p>
  
  <p>When I scroll down my recycler view for the first time, the totalQTY which was previously updating normally, gets reset to 0. However when my go up from here and then scroll down again the time onwards, the totalQTY is now incrementing as it should and never gets reset. In short, the reset happens only when scrolled down the recycler view for the first time.</p>
  
  <p><strong>My ProductRecyclerViewAdapter class</strong></p>
  
  <pre><code>public class ProductRecyclerViewAdapter extends RecyclerView.Adapter&lt;ProductRecyclerViewAdapter.ProductViewHolder&gt;{
  
      private Context context;
      private List&lt;Product&gt; productList;
      private CategoriesFragment fragment;
      private int totalQTY;
      private double totalPrice = 0;
  
   public ProductRecyclerViewAdapter(Context context, List&lt;Product&gt; productList, CategoriesFragment fragment) {
          this.context = context;
          this.productList = productList;
          this.fragment = fragment;
      }
  
      @NonNull
      @Override
      public ProductViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
          LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
          View view = inflater.inflate(R.layout.product_recyclerview_list_item,viewGroup,false);
          ProductViewHolder holder = new ProductViewHolder(view);
          return holder;
      }
  
  @Override
      public void onBindViewHolder(final @NonNull ProductViewHolder prouctViewHolder, int i) {
          final Product product = productList.get(i);
          prouctViewHolder.pdtTitle.setText(product.getTitle());
          prouctViewHolder.pdtPrice.setText("MRP Rs " + String.valueOf(product.getPrice()));
          prouctViewHolder.pdtImageView.setImageDrawable(context.getResources().getDrawable(product.getImageId()));
          totalQTY = Integer.parseInt(prouctViewHolder.counter.getText().toString());   //Initially 0
          prouctViewHolder.qtyminus.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  int qty = Integer.parseInt(prouctViewHolder.counter.getText().toString());
                  if (qty &gt; 0) {
                      qty--;
                      totalPrice = totalPrice - product.getPrice();
                      totalQTY--;
                      fragment.setCheckoutToolbarText(totalQTY,totalPrice);
                      if(totalQTY==0) {
                          fragment.makeCheckoutGone();
                      }
                  } else {
                      Toast.makeText(context, "Cannot have a negative quantity\nCAN YOU?", Toast.LENGTH_LONG).show();
                  }
                  prouctViewHolder.counter.setText(String.valueOf(qty));
              }
          });
          prouctViewHolder.qtyplus.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  int qty = Integer.parseInt(prouctViewHolder.counter.getText().toString());
                  qty++;
  
                  totalPrice += product.getPrice();
  
                  totalQTY++
                  prouctViewHolder.counter.setText(String.valueOf(qty));
                  fragment.setCheckoutToolbarText(totalQTY,totalPrice);
                  fragment.makeCheckoutVisible();
  
              }
          });
  
      }
  
  
      @Override
      public int getItemCount() {
          return productList.size();
      }
  
      class ProductViewHolder extends RecyclerView.ViewHolder {
  
          ImageView pdtImageView;
          TextView pdtTitle, pdtPrice, counter;
          Button qtyminus, qtyplus;
  
          public ProductViewHolder(@NonNull View itemView) {
              super(itemView);
              pdtImageView = itemView.findViewById(R.id.product_imageView);
              pdtTitle = itemView.findViewById(R.id.textViewTitle);
              pdtPrice = itemView.findViewById(R.id.textViewPrice);
              counter = itemView.findViewById(R.id.qty_counter);
              qtyminus = itemView.findViewById(R.id.qty_minus);
              qtyplus = itemView.findViewById(R.id.qty_plus);
  
          }
  
      }
  </code></pre>
  
  <p><strong>My product_recycler_view_list_item.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:orientation="vertical"&gt;
  
  
          &lt;RelativeLayout
              android:layout_width="match_parent"
              android:layout_height="135dp"
              android:background="@drawable/product_background"
              android:layout_marginStart="16dp"
              android:layout_marginEnd="16dp"
              android:layout_marginTop="16dp"&gt;
  
              &lt;ImageView
                  android:id="@+id/product_imageView"
                  android:layout_width="100dp"
                  android:layout_height="100dp"
                  android:layout_alignParentStart="true"
                  android:layout_marginStart="16dp"
                  android:layout_alignParentTop="true"
                  android:layout_marginTop="25dp"
                  android:scaleType="centerInside"
                  android:background="@null"/&gt;
  
              &lt;TextView
                  android:id="@+id/textViewTitle"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_alignParentTop="true"
                  android:layout_marginTop="16dp"
                  android:layout_marginStart="16dp"
                  android:layout_toEndOf="@id/product_imageView"
                  android:text="Banana"
                  android:fontFamily="@font/baloo"
                  android:textSize="20sp"
                  android:textColor="@color/colorAccent" /&gt;
  
              &lt;TextView
                  android:id="@+id/textViewPrice"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_below="@id/textViewTitle"
                  android:layout_marginStart="16dp"
                  android:layout_toEndOf="@id/product_imageView"
                  android:text="Rs 120"
                  android:textStyle="bold"
                  android:textColor="@color/green"/&gt;
              &lt;Button
                  android:id="@+id/qty_minus"
                  android:background="@null"
                  android:text="-"
                  android:textColor="@color/red"
                  android:textSize="20sp"
                  android:layout_width="40dp"
                  android:layout_height="40dp"
                  android:layout_below="@+id/textViewPrice"
                  android:layout_toEndOf="@id/product_imageView"
                  android:layout_marginTop="16dp"/&gt;
  
              &lt;TextView
                  android:id="@+id/qty_counter"
                  android:text="0"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_below="@+id/textViewPrice"
                  android:layout_toEndOf="@id/qty_minus"
                  android:layout_marginTop="26dp"
                  android:layout_marginEnd="16dp"
                  android:layout_marginStart="16dp"/&gt;
  
              &lt;Button
                  android:id="@+id/qty_plus"
                  android:background="@null"
                  android:text="+"
                  android:textColor="@color/red"
                  android:textSize="20sp"
                  android:layout_width="40dp"
                  android:layout_height="40dp"
                  android:layout_below="@+id/textViewPrice"
                  android:layout_toEndOf="@id/qty_counter"
                  android:layout_marginTop="16dp"/&gt;
  
          &lt;/RelativeLayout&gt;
  
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/CtRva.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/CtRva.jpg" alt="**The individual recycler view item preview**"></a></p>
  
  <p>Below is the method, inside the Categories Fragment which gets the totalQTY from the ProductRecyclerViewAdapter's onBindViewHolder method</p>
  
  <pre><code>public void setCheckoutToolbarText(int totalQTY, double totalPrice){
          Log.i("totalQTY",Integer.toString(totalQTY));
  
      }
  </code></pre>
  
  <p>Currently, I have added 6 products to my productList, and 4 are being shown on the screen, the remaining 2 are shown when i scroll down. What i did for the debugging was that i clicked on the + counter of each of the 4 products on the screen individually, so the totalQTY in log increased from 1 to 4.</p>
  
  <p>However when i scrolled down and clicked on the 5th product, the totalQTY reset to 0 inside the onBindViewHolder, got updated on the click and sent 1 back to the above method instead of sending 5.
  Then i clicked on the 6th image, the totalQTY became 2. Then i scrolled up again and clicked on the 1st 4 products, the totalQTY became 3,4,5,6. Then when i scrolled down again, this time the totalQTY rightly showed 7,8 on clicking the last 2 products.</p>
  
  <p>Hence the problem i am facing happens only when i scroll down for the first time below.</p>
  
  <p><strong>The Log.i</strong></p>
  
  <pre><code>2019-07-03 10:11:32.311 17011-17011/com.example.gofresh I/totalQTY: 1
  2019-07-03 10:11:34.150 17011-17011/com.example.gofresh I/totalQTY: 2
  2019-07-03 10:11:35.996 17011-17011/com.example.gofresh I/totalQTY: 3
  2019-07-03 10:11:39.659 17011-17011/com.example.gofresh I/totalQTY: 4
  2019-07-03 10:11:41.015 17011-17017/com.example.gofresh I/example.gofres: Compiler allocated 4MB to compile void android.widget.TextView.&lt;init&gt;(android.content.Context, android.util.AttributeSet, int, int)
  2019-07-03 10:11:41.956 17011-17011/com.example.gofresh I/totalQTY: 1
  2019-07-03 10:11:51.176 17011-17011/com.example.gofresh I/totalQTY: 2
  2019-07-03 10:11:53.421 17011-17011/com.example.gofresh I/totalQTY: 3
  2019-07-03 10:11:56.142 17011-17011/com.example.gofresh I/totalQTY: 4
  2019-07-03 10:11:57.337 17011-17011/com.example.gofresh I/totalQTY: 5
  2019-07-03 10:11:58.545 17011-17011/com.example.gofresh I/totalQTY: 6
  2019-07-03 10:11:59.902 17011-17011/com.example.gofresh I/totalQTY: 7
  2019-07-03 10:12:01.352 17011-17011/com.example.gofresh I/totalQTY: 8
  </code></pre>