views:

68

answers:

3

Hi all, This is my code but i can't fill my string with the value puuted in by the user.

I've tried a lot of solutions from other sites but it won't work.

Somebody please help me! Thanks already

package app.android.Mel

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

public class RedFlashlightActivity extends Activity {

private EditText text;
private TextView myRecord;
private Button myButton;

/** Called when the activity is first created. */
@Override

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

    text = (EditText) findViewById(R.id.txtName);
    myButton = (Button) this.findViewById(R.id.myButton);
    myRecord = (TextView) findViewById(R.id.myRecord);
    final String rec = text.getText().toString();

    myButton.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            myRecord.setText(rec);
            Toast.makeText(RedFlashlightActivity.this,rec, Toast.LENGTH_SHORT).show();
        }

    });
}
}
A: 
    myButton.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        String rec = text.getText().toString();
        myRecord.setText(rec);
        Toast.makeText(RedFlashlightActivity.this,rec, Toast.LENGTH_SHORT).show();
    }
});
ty now it works
Joris
+1  A: 

The point is that rec is created once at the activity creation time and doesn't change ever after( it is final ). Just replace

myRecord.setText(rec);

with

myRecord.setText(text.getText().toString());
Vladimir Ivanov
ty now it works
Joris
mark as resolved please
Vladimir Ivanov
A: 

move the rec = text.getText().toString() into the OnClickListener event handler class.. then it should work.. otherwise it will take a null value.. because ur using the String rec which is constructed during the Activity creation phase..

bala singareddy
ty it works now
Joris