using an EditText as output


Last Updated:

  1. v1nsai

    v1nsai Well-Known Member This Topic's Starter

    Joined:
    Mar 24, 2010
    Messages:
    60
    Likes Received:
    3
    I'm writing a (what was supposed to be) quickie program to flex my unused java muscles and check out android. I'm trying to write a simple celsius to fahrenheit converter, that has 2 TextView labels for F and C, and 2 corresponding EditTexts. When a value is entered in one of the boxes and the convert Button pushed, the empty EditText is filled in with the converted value.

    I've had a surprising amount of difficulty accessing my variables from inside the onClick() handler (or any other method) however. I expected to have to convert the EditText value into a string, and then to a double, but in order to reference any of the variables from onClick I've had to label them final, which has prevented me from modifying any of them.

    So first of all, why is it necessary for these to be final in the first place? I haven't used Java in a while but I know this wouldn't be a problem in C. And second, how should I write this?
     

    Advertisement
  2. gorn

    gorn Well-Known Member

    Joined:
    Mar 22, 2010
    Messages:
    48
    Likes Received:
    11
    It sounds like you are going about it the wrong way, I don't understand what you have now, but I think what you want is something like:

    Code (Text):
    1.  
    2.    private class MyOnClickListener implements OnClickListener {
    3.         public void onClick(View v) {
    4.              EditText input1 = (EditText) findViewById(R.id.inputCell1)
    5.              String s = input1.getText().toString();
    6.              Double d = Double.parseDouble(s);
    7.              Double d2 = d*2;
    8.  
    9.              EditText output = (EditText) findViewById(R.id.outputCell);
    10.              output.setText(d2.toString());
    11.          }
    12.     }
    13.  
     
  3. v1nsai

    v1nsai Well-Known Member This Topic's Starter

    Joined:
    Mar 24, 2010
    Messages:
    60
    Likes Received:
    3
    I tried putting all my variable declarations in the onClick definition but I get force closes whenever I put in a value and click the button. Isn't putting variable definitions in a handler really badwrong? I thought that was strictly for instructions, since it will get called multiple times if you click the button more than once, and cause multiple definitions.

    It's a really simple project so I'm just gonna post what I've got.

    Code (Text):
    1. public class Main extends Activity
    2. {
    3.     /** Called when the activity is first created. */
    4.     @Override
    5.     public void onCreate(Bundle savedInstanceState)
    6.     {
    7.         super.onCreate(savedInstanceState);
    8.         setContentView(R.layout.main);
    9.        
    10.         Button b = (Button)this.findViewById(R.id.button);
    11.         b.setOnClickListener( new OnClickListener()
    12.         {
    13.             public void onClick( View b )
    14.             {
    15.                 EditText fartext = (EditText)findViewById(R.id.fartext);
    16.                 EditText celtext = (EditText)findViewById(R.id.celtext);
    17.                 String farstring = fartext.toString();
    18.                 String celstring = celtext.toString();
    19.                 double far = Double.parseDouble(farstring);
    20.                 double cel = Double.parseDouble(celstring);
    21.                
    22.                 if( fartext.toString() != "0" )
    23.                 {
    24.                     double celsius = (far - 32 ) / 1.8;
    25.                     celtext.setText( celstring.valueOf(celsius) );
    26.                 }
    27.                 if( celtext.toString() != "0")
    28.                 {
    29.                     double fahrenheit = (cel * 1.8) + 32;
    30.                     fartext.setText( farstring.valueOf(fahrenheit));
    31.                 }
    32.             }
    33.         });
    34.     }
    35. }
     
  4. MadBunny

    MadBunny Member

    Joined:
    May 4, 2010
    Messages:
    11
    Likes Received:
    0
    Well I'm guessing that one of the editText boxes is empty and thus when you attempt to do a toString() on the empty box and assign it to a variable it may be freaking out?

    Try setting both boxes to 0 initially and see if it fixes it.
     
  5. v1nsai

    v1nsai Well-Known Member This Topic's Starter

    Joined:
    Mar 24, 2010
    Messages:
    60
    Likes Received:
    3
    At first I figured that was the problem but with or without zeros I still get force closed. I'm going to have to sit down and figure out debugging with eclipse.
     
  6. gorn

    gorn Well-Known Member

    Joined:
    Mar 22, 2010
    Messages:
    48
    Likes Received:
    11
    I don't think you want to call toString on the edit text anyway, I think you want
    Code (Text):
    1. fartext.getText().toString();
    also you can see the exception thrown without eclipse debugging by just doing:

    adb logcat *:E
     
  7. markb

    markb Well-Known Member

    Joined:
    Sep 14, 2009
    Messages:
    148
    Likes Received:
    26
    Hi v1nsai,

    I tried out your code, and stepped through it in the debugger.
    The problem is in these lines:

    Code (Text):
    1.  
    2.                 String farstring = fartext.toString();
    3.                 String celstring = celtext.toString();
    4.  
    They don't return the contents of the text fields, as you might expect.
    I get these values:

    Code (Text):
    1. farstring = android.widget.EditText@43d08e88
    2. celstring = android.widget.EditText@43d0a800
    3.  
    It will then crash when you try to convert those strings to doubles using Double.parseDouble

    To retrieve the contents of the text fields, this works:

    Code (Text):
    1.                 String farstring = fartext.getText().toString();
    2.                 String celstring = celtext.getText().toString();
    3.  
    I hope that helps, and gets you going again.

    Mark

    p.s.

    I just noticed that gorn had already answered your question, so my post is redundant.
    But I'll leave it here anyway. Next time I'll read the whole thread before I dive in!
     
  8. v1nsai

    v1nsai Well-Known Member This Topic's Starter

    Joined:
    Mar 24, 2010
    Messages:
    60
    Likes Received:
    3
    Awesome thanks a bunch guys, I'm going to take some time and get comfortable with the debugger, I've been putting it off for too long hehe.
     

Share This Page

Loading...