title: Getting a single total sum after iterating through a List
body: <p>I am calculating the sum of the price in my List by iterating through with a for loop but it returns the total in a list too. If you don't understand, this is my code for trying looping through the list.</p>
  
  <pre><code>  public class CartAdapter extends ArrayAdapter&lt;Cart&gt; {
  
  private Activity context;
  private List&lt;Cart&gt; carts;
  
  public CartAdapter(@NonNull Activity context, ArrayList&lt;Cart&gt; carts) {
      super(context, R.layout.cart_list_item, carts);
      this.carts = carts;
      this.context = context;
  }
  
  @NonNull
  @Override
  public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
  
      LayoutInflater inflater = context.getLayoutInflater();
  
      View cartViewItems = inflater.inflate(R.layout.cart_list_item, null, false);
  
      TextView textViewName = cartViewItems.findViewById(R.id.cart_item);
      TextView textViewPrice = cartViewItems.findViewById(R.id.cart_price);
      ImageView imageViewCart = cartViewItems.findViewById(R.id.cart_img);
  
      Cart cart = carts.get(position);
      textViewName.setText(cart.getName());
      textViewPrice.setText(String.valueOf(cart.getPrice()));
      Picasso.with(context).load(cart.getImg()).into(imageViewCart);
  
      countCart(textViewPrice);
  
      return cartViewItems;
  }
  
  public double countCart(TextView textViewPrice){
      double sum = 0.0;
      for (int i = 0; i &lt; carts.size(); i++) {
          sum = sum + Double.parseDouble(textViewPrice.getText().toString());
      }
      System.out.println("Count Price: " + sum);
  
      return sum;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>and the size of my list at the moment has 2 items inside and this is the output for it.</p>
  
  <blockquote>
    <p>08-10 09:38:15.771 6285-6285/com.zack.tinga.applicationtest I/System.out: Count Price: 23.98</p>
    
    <p>08-10 09:38:15.790 6285-6285/com.zack.tinga.applicationtest I/System.out: Count Price: 23.98</p>
  </blockquote>
  
  <p>The price is correct but the problem is that it returns the sum using the size of the list. How can I make it to return just a single sum after iterating through the list. e.g. if the size of the list is 15, then it will print the sum 15 times on the output, which it should just print it once.</p>
  
  <p>This is the screenshot of the app:</p>
  
  <p><a href="https://i.stack.imgur.com/s45Ko.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/s45Ko.png" alt="app screen shot"></a> </p>
  
  <p>So it should display a single total, not list.size(); times as it currently does.</p>