Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

Capturing Price from an Edit Text

Any ideas on how to do this? I am trying to input the value into a SQL database. Again, examples are appreciated.

3 Answers

Ben strikes again! lol Yeah - no, I can retrieve a String from an Edit text. I'm trying to capture "price" from an edit text (i.e., User please input fee: ______ ).

I'm trying to format the Edit text so the User can only input USD (ie ($2.50) ) and I will store that value in my database (which only takes Integers or Reals).

I feel like I need to do a conversion like what I did with the checkbox boolean value (i.e. storing the value as a 1 or 0) but I'm a bit confused as to how to do it. I've read that BigDecimal is the way to go but I don't know how to correctly use that method. I've been looking online but I can't seem to find any code examples which to me is a bit odd considering most forms must capture "price" or "fee" (I would think).

Ben Jakuben
Ben Jakuben
Treehouse Teacher

Ah! If only it were the simple answer. :)

First, I think you will want to choose one of the number inputTypes. Then you'll need to add a TextWatcher to the field to format and check it. Here's an example: http://www.blog.nathanhaze.com/inserting-currency-in-a-edit-text-field-with-text-watcher-android/

With the string you build there you could use BigDecimal which is good for storing monetary values. BigDecimal is a lot like double except it's a full-fledged object that requires a constructor. You can insert BigDecimal values for REAL values in the db.

For a code example, check out the code from my recent workshop on SQLite.

Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

To get the String value from an EditText, do the following:

String inputText = mEditText.getText().toString();

If I misunderstood the question just let me know. :)

Thanks Ben...I'm working it now. I'll let you know how I end up.