views:

87

answers:

2

Humm... I can do this easily in Java (and all other languages) but, it doesn't seem to work in Android.

I simply want to get an integer number input from the user, press a button and do some simple math on it, then display it.

I'm using a TextEdit (also tried a TextView) and have tried many different approaches but, none work. The code itself has no errors and I can do the math and display it if using constants.

The problem is in either parsing it to an integer (though, the code-line seems ok, even though it's a tad different than what I use in pure java.

Any help is welcome - I'm sure it's simple and I read many related posts for this, including the Android Dev site - but...

Here's the short complete code:

package com.bt.strint;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class string_integer extends Activity {
    /** Called when the activity is first created. */   
    int aa = 3;
    int y=33;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

      final TextView input = (TextView) findViewById(R.id.EditText01);           
      final Button btn = (Button) findViewById(R.id.Button01);
      final TextView showMe = (TextView) findViewById(R.id.TextView01); 

      btn.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            //showMe.setText("meow");       //ok            
            //showMe.setText(String.valueOf(y * 7)); // ok
            y = Integer.parseInt((String) input.getText());
            showMe.setText(String.valueOf(y));
        }
      });       
    }
}
+3  A: 

You want to use input.getText().toString(), instead of (String) input.getText(); you can't just cast a CharSequence to String.

Daniel Lew
A: 

I clicked the checkbox (am registered) but I didn't see my "thanks" show up - I'll do it this way because I wanted to try it again - your response was perfect and fast - "Thanks!"

headscratch
Welcome to SO! Don't worry, it worked. There's a big friendly green checkmark under his answer. Also, this kind of "thanks" message would be better suited to a comment than an answer. Click "add comment" under the answer you're responding to.
MatrixFrog