• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Displaying Decimals with ListView

Hello, I am creating a program as a learning experience.

The program keeps a running balance of individual decimal values.

What is displayed is the balance total, and below that is a list of
the individual entries.

The numbers are all double data type, and saved in the sqlite3
database as datatype "REAL NOT NULL".

When the list is displayed, the balance shows the value correctly,
without rounding or cutting off numbers.

However, the list item numbers get rounded, and as the numbers get
bigger, the decimals start getting cut off.

Any ideas?

The xml for the textview:
[XML]<TextView android:id="@+id/txtamt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#000000"
android:gravity="right"
android:textSize="12px"
android:layout_marginRight="10px"/>
[/XML]

And the textview gets populated pulling the data from the database
like this:
Code:
private void fillData() {
    /* Get all of the rows from the database and create the item list
*/
    /* for mult accts, pass in acct name? */
    mEntryCursor = mDbHelper.fetchAllEntries();
    startManagingCursor(mEntryCursor);

    // Create an array to specify the fields we want to display in the
list (only TITLE)
    String[] from = new String[]
{myDbAdapter.KEY_NMBR,myDbAdapter.KEY_DATE,myDbAdapter.KEY_DESCR,myDbAdapter.KEY_AMT};

    // and an array of the fields we want to bind those fields to (in
this case just text1)
    int[] to = new int[]{R.id.txtnmbr, R.id.txtdate,
R.id.txtdescr, R.id.txtamt};

    // Now create a simple cursor adapter and set it to display
    SimpleCursorAdapter entries =
          new SimpleCursorAdapter(this, R.layout.entryrow,
mEntryCursor, from, to);
     setListAdapter(entries);

  }
 
I did some more testing, and numbers in the format ####.## show up correctly, but numbers in the format #####.## and greater get rounded, and are formatted as #####.#.

What is even stranger, which tells me it is not the amount of digits in the number, is that numbers in the format ####### show up correctly. It has something to do with the combination of 5 digits or more on the left-hand of the decimal point, and two or more digits on the right-hand of the decimal point.

If I then use a number in the format #######.##, or 1533335.35 for example, when displayed in the list, it is automagically formatted as 1.53334e+06...
 
With help from Mark on the 'Android Beginners' Google group, I was able to get it working.

Here is what I did, also referencing GTalk :: anddev.org - Android Development Community | Android Tutorials.

Code:
public class testListView extends ListActivity {
  ...
  private DecimalFormat myCustDecFormatter = new DecimalFormat("########.00");
  ...
  ...
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState)  {
    ...
    ...
    ...
  }

private void fillData() {
    /* Get all of the rows from the database and create the item list */
    /* for mult accts, pass in acct name? */
    mEntryCursor = mDbHelper.fetchAllEntries();
    startManagingCursor(mEntryCursor);
    
    // Create an array to specify the fields we want to display in the list (only TITLE)
    String[] from = new String[]{myDbAdapter.KEY_NMBR,myDbAdapter.KEY_DATE,myDbAdapter.KEY_DESCR,myDbAdapter.KEY_AMT};
    
    // and an array of the fields we want to bind those fields to (in this case just text1)
    int[] to = new int[]{R.id.txtnmbr, R.id.txtdate, R.id.txtdescr, R.id.txtamt};
    
    // Now create a simple cursor adapter and set it to display
    setListAdapter(new SimpleCursorAdapter(this, R.layout.entryrow, mEntryCursor, from, to) {
        @Override
        public void setViewText(TextView v, String text) {
          super.setViewText(v, convText(v, text));
        }
        
    });
     
  }
  
  private String convText(TextView v, String text) {
    switch (v.getId()) {
      case R.id.txtamt:
        double dblAmt;
        //dblAmt = Double.valueOf(text);
        dblAmt = mEntryCursor.getDouble(AMT_COLUMN);
        return myCustDecFormatter.format(dblAmt);
    }
      return text;
    } 
...
...
...
}//end testListView
 
Back
Top Bottom