0

I'm new to android and am writing a simple app that calculates the distance left to the hole after a shot. Everything runs with out errors or warnings but I can't seem to get the editText values to be stored in the variables. What am I doing wrong? The comments in the code are other things Ive tried. thanks for the help.

package my.pckg.ezgolf;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.EditText;
import android.widget.TextView;

public class EZGolfActivity extends Activity {
    /** Called when the activity is first created. */
    int hole_length;
    int shot_length;
    int dist_remain;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        int dist_remain = hole_length - shot_length;

    final EditText edittext = (EditText) findViewById(R.id.editText1);
    edittext.setOnKeyListener(new OnKeyListener() {
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            // If the event is a key-down event on the "enter" button
            if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
                (keyCode == KeyEvent.KEYCODE_ENTER)) {
              // Perform action on key press
              //Toast.makeText(EZGolfActivity.this, edittext.getText(), Toast.LENGTH_SHORT).show();
                hole_length = Integer.parseInt(edittext.getText().toString());

              return true;
            }
            return false;}               

            TextView myTextView = (TextView) findViewById(R.id.textView4); 
            myTextView.setText("You have this many feet to the pin: " + dist_remain);



    });

    final EditText edittext2 = (EditText) findViewById(R.id.editText2);
    edittext.setOnKeyListener(new OnKeyListener() {
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            // If the event is a key-down event on the "enter" button
            if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
                (keyCode == KeyEvent.KEYCODE_ENTER)) {
              // Perform action on key press
              //Toast.makeText(EZGolfActivity.this, edittext.getText(), Toast.LENGTH_SHORT).show();
                shot_length = Integer.parseInt(edittext2.getText().toString());
              return true;
            }
            return false;}


            //TextView myTextView = (TextView) findViewById(R.id.textView4); 
            //myTextView.setText("You have this many feet to the pin: " + dist_remain); 

        //int dist_remain = hole_length - shot_length;

        TextView myTextView = (TextView) findViewById(R.id.textView4); 
        myTextView.setText("You have this many feet to the pin: " + dist_remain); 


});

    //int dist_remain = hole_length - shot_length;

    //TextView myTextView = (TextView) findViewById(R.id.textView4); myTextView.setText("You have this many feet to the pin: " + dist_remain);

}

}

Chris Tetreault
  • 1,973
  • 13
  • 19

4 Answers4

1

Seems, that your calculation and the update of myTextView are only called once at the initialization of the Activity. You have to move the calculation and call to myTextView.setText into the KeyListeners. This is untested, but I think, this will work. At least the syntax erorrs should be fixed. As you can see, the calculation is done each time the enter button is pressed and only then the text view is updated.

public class EZGolfActivity extends Activity {
    /** Called when the activity is first created. */
    int hole_length;
    int shot_length;
    int dist_remain;

    TextView myTextView = (TextView) findViewById(R.id.textView4);

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

        final EditText edittext = (EditText) findViewById(R.id.editText1);
        edittext.setOnKeyListener(new OnKeyListener() {
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                // If the event is a key-down event on the "enter" button
                if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
                    // Perform action on key press
                    // Toast.makeText(EZGolfActivity.this, edittext.getText(), Toast.LENGTH_SHORT).show();
                    hole_length = Integer.parseInt(edittext.getText().toString());
                    int dist_remain = hole_length - shot_length;
                    myTextView.setText("You have this many feet to the pin: " + dist_remain);
                    return true;
                }
                return false;
            }
        });

        final EditText edittext2 = (EditText) findViewById(R.id.editText2);
        edittext.setOnKeyListener(new OnKeyListener() {
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                // If the event is a key-down event on the "enter" button
                if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
                    // Perform action on key press
                    // Toast.makeText(EZGolfActivity.this, edittext.getText(), Toast.LENGTH_SHORT).show();
                    shot_length = Integer.parseInt(edittext2.getText().toString());
                    int dist_remain = hole_length - shot_length;
                    myTextView.setText("You have this many feet to the pin: " + dist_remain);
                    return true;
                }
                return false;
            }
        });
    }
}
henrik
  • 708
  • 7
  • 14
  • I did so..giving me syntax errors, do I have to reload the view as well? – Chris Tetreault Jun 16 '12 at 16:50
  • No, you should not need to reload the view. The call to setText should update the View immediately. Can't help you with the syntax errors unless you name them. – henrik Jun 16 '12 at 16:53
  • Syntax error on tokens, AnnotationName expected instead - Syntax error, insert "enum Identifier" to complete EnumHeaderName these are coming from mytextView.settext line – Chris Tetreault Jun 16 '12 at 16:59
0

I have one point not big one

you have set setOnKeyListener both time for edittext

Dheeresh Singh
  • 15,643
  • 3
  • 38
  • 36
0

I don't think you want to use getInteger. You were on a better path with parseInt:

Why does int num = Integer.getInteger("123") throw NullPointerException?

Community
  • 1
  • 1
DrA
  • 437
  • 2
  • 11
0

You could try using the addTextChangedListener method of the EditText.

 EditText txt_value = (EditText) findViewById(R.id.txt_value);
 txt_value.addTextChangedListener(new TextWatcher() 
    {
        public void  afterTextChanged (Editable s)
        {
            //Log.d("after", "afterTextChanged");
        }
        public void  beforeTextChanged  (CharSequence s, int start, int count, int after)
        {
            //Log.d("before", "beforeTextChanged");
        }
        public void onTextChanged(CharSequence s, int start, int before, int count) 
        {
            txt_value.removeTextChangedListener(this);
            String textValue = s.toString();
            int intValue = Integer.parseInt(textValue);
        }
    });
cking24343
  • 3,173
  • 1
  • 21
  • 23