0

I am trying to display the result of a calculation on a TextView or EditText. I get the users input from one100lbs and tenPounds and then add it together and am trying to display it on totalPounds. Its is not the equation I am going to use but just want to see that it works. Currently with the code below my application crashs. This is all under one activity. Also how come when I change the ID of a EditText the location of the editText changes on my relative layout? Please no links, i know its simple but I am a noob. I have searched and am having a hard time finding a solution.

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.pounds);
    addListenerOnSpinnerItemSelection();

    EditText one100lbs = (EditText) findViewById(R.id.one100lbs);
    int one = Integer.valueOf(one100lbs.getText().toString());

    EditText tenPounds = (EditText) findViewById(R.id.tenPounds);
    int two = Integer.valueOf(tenPounds.getText().toString());

    int result = one + two;

    TextView textView = (TextView) findViewById(R.id.totalPounds);
    textView.setText(result);   
}
Avadhani Y
  • 7,566
  • 19
  • 63
  • 90
kanga
  • 35
  • 2
  • 5
  • 2
    ID has nothing to do with layout, firstly. Second, if your application crashes, please provide the (red) LogCat logs produced by the crash. – Cat Jan 14 '13 at 23:38

2 Answers2

4

You want something like:

textView.setText(String.valueOf(result));

As it stands, Android is trying to find a resource id when you supply just an int, which will fail.

It has also occured to me that you're using EditTexts, which are notorious for failing when you input numbers, besides forcing the keypad to be only numbers, you can do something like:

int one = 0;
int two = 0;

try{
  EditText one100lbs = (EditText) findViewById(R.id.one100lbs);
  one = Integer.valueOf(one100lbs.getText().toString().trim());
}
catch (NumberFormatException e)
{
  one = -1;
}

try{
  EditText tenPounds = (EditText) findViewById(R.id.tenPounds);
  two = Integer.valueOf(tenPounds.getText().toString().trim()); 
}
catch (NumberFormatException e)
{
  two = -1;
}

int result = one + two;

TextView textView = (TextView) findViewById(R.id.totalPounds);
textView.setText(String.valueOf(result)); 
Community
  • 1
  • 1
A--C
  • 36,351
  • 10
  • 106
  • 92
  • Or you can use the more concise (and oft-used) `textView.setText(result + "");` But the idea remains the same, cast it to a String and you should be good. – syklon Jan 14 '13 at 23:41
  • @zyklonSport I have seen `String.valueOf()` being recommended more than `+""`, so I have adapted accordingly. – A--C Jan 14 '13 at 23:43
  • 1
    Thought I'd check and it seems the community [agrees](http://stackoverflow.com/questions/1572708/is-conversion-to-string-using-int-value-bad-practice) with you. +1 for knowledge – syklon Jan 15 '13 at 00:00
  • Thank You for the quick responses. When i used both of the codes above it does not like the "textView" and has a red undelrine quoting "textView cannot be resolved" is textView supposed to be the ID of my result textView? If it is it still put the ID name and still has a red underline. How do i make java recognize that ID name? – kanga Jan 15 '13 at 01:12
  • @user1978756 did you first do `TextView textView = (TextView) findViewById(R.id.totalPounds);` then added my line? `textView` (with lowercase t) is the variable that holds a `TextView` (uppercase T) object. If you haven't imported the `TextView` class, import `android.widget.TextView`. – A--C Jan 15 '13 at 01:14
  • It Works! Thank You! If I want it to recalculate every time the user changes the input(text change), which listener method would I use? – kanga Jan 15 '13 at 20:35
  • @user1978756 if you want it to be dynamic, you would use a [TextWatcher](http://developer.android.com/reference/android/text/TextWatcher.html). Also, since this answer has answered your question, you should mark it as accepted. – A--C Jan 15 '13 at 20:42
0

You can use either one of the following:

textView.setText(Integer.toString(result));

or

textView.setText(result + "");
Fergers
  • 473
  • 2
  • 7
  • 25