I am trying to get the value of the Strings firstChoice and secondChoice from my Button onClick method below and use it in the same class. The problem is that I know you can’t return a String value from a void method. Please take a look at what I have below:
public class Question extends Activity implements OnClickListener{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_question);
Choice1 = (Button) findViewById(R.id.Choice4);
Choice1.setOnClickListener(Question.this);
Choice2 = (Button) findViewById(R.id.Choice5);
Choice2.setOnClickListener(Question.this);
firstChoice;
secondChoice;
public void onClick(View view) {
switch(view.getId()) {
case R.id.Choice4:
showSelectPicksDialog();
Button b = (Button)view;
String firstChoice = b.getText().toString();
break;
case R.id.Choice5:
showSelectPicksDialog2();
Button v = (Button)view;
String secondChoice = v.getText().toString();//
break;
default:
break;
}
}
Is there a way I can get those values? As it is right now, When I try to call the values in my on Create method, I get the error saying that firstChoice and secondChoice “cannot be resolved to a type”.
There are many ways, you can use a class variable for instance: