title: SUM sqlite column double values in android
body: <p>I must get the sum of the values of a column, but I get the number of the rows of the table.</p>
  
  <p>Here is the code that I use to retrieve the sum value:</p>
  
  <pre><code>Cursor c=sqLite.getPrezzoTotaleCarrello();
  
          while(c.moveToNext())
          {
              prezzoTotaleCarrello.setText(c.getString(c.getColumnIndex("Somma_totale"))); 
              Log.d(prezzoTotaleCarrello.getText().toString(),"Visualizziamo");
          }
  </code></pre>
  
  <p>Here is the code of the getPrezzoTotaleCarrello() method:</p>
  
  <pre><code>public Cursor getPrezzoTotaleCarrello()
      {
          String query = "select SUM(Prezzo_totale) AS Somma_totale  from Carrello";
  
          return getReadableDatabase().rawQuery(query, null);
      }
  </code></pre>
  
  <p>The method getPrezzoTotaleCarrello is inside a class extending SQLiteOpenHelper.</p>
  
  <p>Prezzo_totale is a column that contains double values.
  It's incredible I've tried the same code with a column containing integer values and it works.
  I don't understand why it happens.</p>
  
  <p>I had to use cursor.getDouble() instead of cursor.getString().
  Now there is another problem the part after the dot is cut. For example if the value is 2.55 I see in the TextView 2.00. </p>
  
  <p>Here is the new code:</p>
  
  <pre><code>Cursor c=sqLite.getPrezzoTotaleCarrello();
  
          while(c.moveToNext())
          {
              prezzoTotaleCarrello.setText(String.valueOf(String.format("%.2f",c.getDouble(c.getColumnIndex("Somma_totale")))));
              Log.d(prezzoTotaleCarrello.getText().toString(),"Visualizziamo");
          }
  </code></pre>
  
  <p>New code:</p>
  
  <pre><code>while(c.moveToNext())
          {
             // prezzoTotaleCarrello.setText(String.valueOf(String.format("%.2f",c.getDouble(c.getColumnIndex("Somma_totale")))));
  
              Double total = c.getDouble(0);
              prezzoTotaleCarrello.setText(String.valueOf(String.format("%.2f",total)));
  
              Log.d(prezzoTotaleCarrello.getText().toString(),"Visualizziamo");
          }
  </code></pre>
  
  <p>Here is all the code:</p>
  
  <pre><code>class custom_list_one extends BaseAdapter
  {
      private LayoutInflater layoutInflater;
      ViewHolder holder;
      private ArrayList&lt;Prodotti&gt; cartList=new ArrayList&lt;Prodotti&gt;();
      int cartCounter;
      Typeface type;
      Context context;
      NumberFormat format = NumberFormat.getInstance(Locale.ITALY);
  
      public custom_list_one(Context context, ArrayList&lt;Prodotti&gt; cart_list) {
          layoutInflater = LayoutInflater.from(context);
          this.cartList=cart_list;
          this.cartCounter= cartList.size();
          this.context = context;
          //type= Typeface.createFromAsset(context.getAssets(),"fonts/book.TTF");
      }
  
      @Override
      public int getCount() {
  
          return cartCounter;
      }
  
      @Override
      public Object getItem(int arg0) {
  
          return cartList.get(arg0);
      }
  
      @Override
      public long getItemId(int arg0) {
  
          return arg0;
      }
  
      @Override
      public View getView(final int position, View convertView, ViewGroup parent) {
          Prodotti tempProdotti = cart_list.get(position);
  
          if (convertView == null)
          {
              convertView = layoutInflater.inflate(R.layout.listone_custom, null);
              holder = new ViewHolder();
              holder.nome = (TextView) convertView.findViewById(R.id.product_name);
              holder.quantita_scelta= (TextView) convertView.findViewById(R.id.quantita_scelta_carrello);
              holder.cancel = (ImageButton) convertView.findViewById(R.id.delete);
              holder.prezzo = (TextView) convertView.findViewById(R.id.prezzo);
  
  
              convertView.setTag(holder);
          }
          else
          {
              holder = (ViewHolder) convertView.getTag();
  
          }
  
          holder.nome.setText(tempProdotti.getNome());
          holder.nome.setTypeface(type);
  
          holder.quantita_scelta.setText(tempProdotti.getQuantita());
          holder.quantita_scelta.setTypeface(type);
  
  
          holder.prezzo.setText(tempProdotti.getPrezzo());
          holder.prezzo.setTypeface(type);
  
  
  
  
              //otteniamo la somma di tutti i prezzi dei prodotti del carrello
  
          Double somma=sqLite.getPrezzoTotaleCarrello();
          Log.d(String.valueOf(somma),"Visualizziamoooooooo");
          String sommaStringa= String.valueOf(somma);
          prezzoTotaleCarrello.setText(String.valueOf(sommaStringa));
             /*
              Cursor c=sqLite.getPrezzoTotaleCarrello();
          while(c.moveToNext())
          {
             // prezzoTotaleCarrello.setText(String.valueOf(String.format("%.2f",c.getDouble(c.getColumnIndex("Somma_totale")))));
  
              Double total = c.getDouble(0);
              prezzoTotaleCarrello.setText(String.valueOf(String.format("%.2f",total)));
  
              Log.d(prezzoTotaleCarrello.getText().toString(),"Visualizziamo");
          }*/
  
  
  
  
  
             // Number number2=format.parse(tempProdotti.getPrezzo());
              //Double prezzoTotaleProdotto=number2.doubleValue();
  
              //aggiorniamo la casella di testo relativa alla somma totale dei prezzi dei prodotti del carrello
             // prezzoTotaleCarrello.setText( String.valueOf(String.format("%.2f", (prezzoTotale+prezzoTotaleProdotto))));
            /*
          } catch (ParseException e) {
              e.printStackTrace();
          }*/
  
  
  
          holder.cancel.setOnClickListener(new MyPersonalClickListener("button_delete", tempProdotti));
  
          return convertView;
      }
      class ViewHolder
      {
          TextView nome;
          TextView prezzo;
          TextView quantita_scelta;
          ImageButton cancel;
      }
  
  }
  </code></pre>