Reputation: 25
I am retrieving the value of a TextView like this
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.calculation);
TextView heading = (TextView) findViewById(R.id.paper_name);
......
}
I have another method in same class
@Override
public void onClick(View v) {
int BLACK_AND_WHITE_MULTIUPLIER = 4200;
int COLOR_MULTIUPLIER = 6400;
switch (v.getId()) {
case R.id.btCalculate:
int multiplier = rbColor.isChecked() ? COLOR_MULTIUPLIER : BLACK_AND_WHITE_MULTIUPLIER;
int column = Integer.parseInt((String) spColumn.getSelectedItem());
int inch = Integer.parseInt((String) spInch.getSelectedItem());
tvAmount.setText((multiplier * column * inch) + "");
break;
}
}
I want to set the value of COLOR_MULTIUPLIER
and BLACK_AND_WHITE_MULTIUPLIER
based on value of heading which I have got from the onCreate method. Is it possible?
Upvotes: 3
Views: 3971
Reputation: 3445
Like I said in my comment, you just need to make heading
a variable that belongs to the whole object, not just the onCreate
method. You do this in the following way.
public class MyActivity extends Activity {
TextView heading; //Declared outside of onCreate
protected void onCreate(Bundle savedInstanceState) {
heading = (TextView) findViewById(R.id.paper_name); //Assigned inside of onCreate
}
}
Then that variable becomes accessible inside your onClick
public void onClick(View v) {
heading.getText(); //Or whatever you need to use it for
}
Upvotes: 1
Reputation: 2735
Declare your TextView heading outside of the onCreate method i.e. at class level. Inside onCreate assign it a value and Now in onClick method can access the varaible heading and get it's value.
Upvotes: 1
Reputation: 9700
Declare your TextView
at class level...
TextView heading;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.calculation);
heading = (TextView) findViewById(R.id.paper_name);
heading.setText("something");
......
then access it in onClick()
method as below...
@Override
public void onClick(View v)
{
int BLACK_AND_WHITE_MULTIUPLIER = 4200;
int COLOR_MULTIUPLIER = 6400;
switch (v.getId())
{
case R.id.btCalculate:
int multiplier = 0;
if (heading.getText().toString.equals("something")) {
multiplier = COLOR_MULTIUPLIER;
} else {
multiplier = BLACK_AND_WHITE_MULTIUPLIER;
}
//int multiplier = rbColor.isChecked() ? COLOR_MULTIUPLIER
// : BLACK_AND_WHITE_MULTIUPLIER;
int column = Integer.parseInt((String) spColumn
.getSelectedItem());
int inch = Integer.parseInt((String) spInch.getSelectedItem());
tvAmount.setText((multiplier * column * inch) + "");
break;
}
}
Upvotes: 1
Reputation: 44813
Move the TextView heading;
so that it is a class variable not a local variable and then it will be accessible throughout your whole class.
It can then be intialized in onCreate
and read and or updated in onClick
Upvotes: 1