Solved

Updating Text Issue

Posted on 2014-11-02
3
87 Views
Last Modified: 2014-11-02
Hi,

I have a tip calculator app in Android - I have coded it so I have the option to split the total and tip by x number of guests.

I have a textview with the default text of "Tip". If the number of guests changes - I want the text to be "Split Tip".

In my app, I have a textWatcher

   
private TextWatcher numGuestTextWatcher = new TextWatcher() 
   {
      @Override
      public void onTextChanged(CharSequence s, int start, 
         int before, int count) 
      {         
         // convert billEditText's text to a double
         try
         {
            numGuests = Integer.parseInt(s.toString());
         } // end try
         catch (NumberFormatException e)
         {
            numGuests = 1; // default if an exception occurs
         } // end catch 
         updateTextFields();
      } //

Open in new window

      
Then the updateTextFields method:
        
	     private void updateTextFields()
   {
	   if (numGuests > 1) {
		   tipTextView.append("Split Tip");
	   }
   }

Open in new window

 
 But when I change the value back to 1, the default text does not change back to "Tip" which is what I set the default string text to.

Any clue why?
0
Comment
Question by:Computer Guy
  • 2
3 Comments
 
LVL 14

Expert Comment

by:CPColin
ID: 40418227
You don't have any code that fires when numGuests is less than or equal to 1. If you put an else block in that updateTextFields() method, you should be able to get the behavior you're looking for.
0
 
LVL 3

Author Comment

by:Computer Guy
ID: 40418372
Like this:

{
         if (numGuests > 1) {
               tipTextView.append("Split Tip");
else
tipTextView.append("Tip");

         }
   }
0
 
LVL 14

Accepted Solution

by:
CPColin earned 500 total points
ID: 40418374
That won't compile. The else has to be after the end of the if block.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

A short article about problems I had with the new location API and permissions in Marshmallow
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now