Search code examples
androidsqlitelistviewcursorsimplecursoradapter

Change the number display in SimpleCursorAdapter


I need to display in a list view some data about numbers. I get these data from a database that is sqlite.

Im using a simplecursoradapter to fill the listview. The problem is that numbers >= to one million are stored in exponential format. So for example if I insert a number like 1000 000 then it gets stored as 1+06E. The problem is that i need to display it as 1 000 000. But I can't manage to change that format inside the cursor or the simplecursoradapter.

My code for filling the listview is:

Cursor c = admin.obtenerCursorGastosVarFecha(fechaSelUs);

      // The desired columns to be bound
      String[] columnas = new String[] {"_id", "Descripcion", "Costo", "Fecha_Creado"};

      // the XML defined views which the data will be bound to
      int[] views = new int[] {R.id.IdGstVar, R.id.DescGstVar, R.id.CostoGstVar, R.id.FechaGstVar };

      // create the adapter using the cursor pointing to the desired data 
      //as well as the layout information
      dataAdapter = new SimpleCursorAdapter(this, R.layout.lista_gastos_variables, c, columnas, views, 0);

Im getting here all as a string, but numbers higher or equal to one million show up in exponential numbers

I tried this code, but its not working at all.

It just modifies values lower than ten million and also, it rounds or changes the values, so if insert for example 8 888 888 it displays 8 888 890.

I need to display in natural numbers all the numbers stored in the database.

Also if there is a way to insert rows with numbers into the database without them being changed to exponential format, how is it done? Thank you very much for your help!

dataAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
            @Override
            public boolean setViewValue(View view, Cursor cursor, int column) {

                if( column == 2 ){ // let's suppose that the column 0 is the date
                    TextView tv = (TextView) view;

                    // here you use SimpleDateFormat to bla blah blah
                    tv.setText(""+Double.parseDouble(cursor.getString(cursor.getColumnIndex("Costo"))));
                    return true;
                }




                return false;
            }
        });

Solution

  • Since you are using datatype as real , and storing it into db as real number. At the time of storing into SqliteDB, it converts it into exponential format. When you are trying to read it from database it gives you the converted exponential format into string. You just change the datatype to varchar for costo. It will solve your problem.