Go Back   Android Forums > Android Development > Application Development

Application Development Dev Lounge for the Coder Folks



Reply
 
LinkBack Thread Tools
Old August 5th, 2009, 11:52 AM   #1 (permalink)
New Member
 
Join Date: Aug 2009
Posts: 3
 
Device(s):
Thanks: 0
Thanked 0 Times in 0 Posts
Question 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);

  }

skatalicious is offline  
Last edited by skatalicious; August 5th, 2009 at 05:49 PM.
Reply With Quote
Sponsors
Old August 5th, 2009, 01:17 PM   #2 (permalink)
New Member
 
Join Date: Aug 2009
Posts: 3
 
Device(s):
Thanks: 0
Thanked 0 Times in 0 Posts
Default Display Number in List

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...
skatalicious is offline  
Reply With Quote
Old August 5th, 2009, 05:47 PM   #3 (permalink)
New Member
 
Join Date: Aug 2009
Posts: 3
 
Device(s):
Thanks: 0
Thanked 0 Times in 0 Posts
Default With help from others, I got it working

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
skatalicious is offline  
Last edited by skatalicious; August 5th, 2009 at 05:50 PM.
Reply With Quote
Reply

Bookmarks

Tags
adapter, android, application, cursor, data, decimal, display, enter, entry, format, item, java, layout, list, listview, number, precision, problem, question, round, row, sql, table, textview, xml


Go Back   Android Forums > Android Development > Application Development User CP
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
regarding getting stuff from backend and displaying them as ListView in a Tab learning_intern Developer 101 1 August 11th, 2010 03:50 AM
ListView purnachandrarao Developer 101 1 July 22nd, 2010 08:46 AM
ListView Amon Darthir Application Development 0 April 2nd, 2010 07:09 PM
listview hanuman495 Android Applications 0 March 19th, 2010 02:31 AM
kml in listview android87 Application Development 0 October 17th, 2009 09:55 AM



All times are GMT -5. The time now is 12:28 AM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
Custom vBulletin Skins by: Relivo


SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.