Reputation: 295
How do or where do I define variables that can be used throughout an activity? I have three onClick Listeneners that contain a bunch of if else statements within each. One onClick causes a bunch a calaculations to occur, another onClick saves results of the calculations using out.write instructions. I am having a problem getting double variables from within if else calcuations of an onClick routine to be known to the save onClick portion of my code. I have been reading books and looking for answers but nothing seems to work. I am using a breakpoint to stop before an out.write that contains a variable from my execution onClick routine but they are unknown values. I first thought the variables need to be global but I just need them to be available throughtout the activity.
I have read plenty of books and tried to comprehend but I jumped into java and wrote 3000+ lines. Everything works as far as getting input, deciding what the inputs were, and displaying the results. I even create a nice .xml file on the sdcard showing all the data results. The problem is when I try and put a variable in the out.write code the variable m1_sqs1_eng is unknown in that onCreate routine. I had my variables defined like your example but it did not work. Some code example of my problem:
public class MyActivity extends Activity {
private EditText m1_sqs1;
private Spinner m1_sqs1_spinner;
private double m1_sqs1_eng;
private double m1_sqs1_value;
protected onCreate( Bundle savedBundle ) {
m1_sqs1_spinner = (Spinner) findViewById(R.id.m1_sqs1_spinner);
m1_sqs1 = (EditText) findViewById(R.id.m1_sqs1);
btnExecute.setOnClickListener(new View.OnClickListener() {
if (((m1_sqs1_spinner.getSelectedItem().toString().equals("in")))) {
double m1_sqs1_eng = new Double(m1_sqs1.getText().toString());
});
btnSave.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
m1_sqs1_value = m1_sqs1_eng;
out.write (" <Cell ss:StyleID=\"s44\"><Data ss:Type=\"Number\">" + m1_sqs1_value + "</Data><NamedCell ss:Name=\"Print_Area\"/></Cell>\r\n");
});
}
}
Upvotes: 2
Views: 153
Reputation: 38706
For example:
public class MyActivity extends Activity {
private EditText streetField;
private EditText nameField;
protected onCreate( Bundle savedBundle ) {
streetField = findViewById( R.id.streetField );
nameField = findViewVById( R.id.nameField );
someButton.setOnClickListener( new View.OnClickListener() {
public void onClick( View view ) {
Person person = new Person();
person.setName( nameField.getText().toString() );
person.setStreet( streetField.getText().toString() );
}
});
}
}
Here streetField and nameField are instance variables for the MyActivity class. They can be used by in any instance method, inner class, or anonymous inner classes defined in instance methods. You probably want to pick up a introductory Java book because most of this is discussed in those books.
http://java.sun.com/docs/books/effective/
Upvotes: 3
Reputation: 11230
You should define class level variables in the parent activity and access them in onClickListeners.
If this Listener classes are anonymous inner classes you can access this variable directly. Or if these are implemented as a public class, then you can pass the activity reference to these listeners while creating them.
Upvotes: 2