So far I have been able to successfully pass a string using this method, however it seems to not be working when I try to pass an integer.
Class that is setting the integer has the following:
My Helper
public int getHoles() {
return setHoles;
}
My Button
case R.id.buttonSetHoles:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
final CharSequence[] items = {"18", "9"};
builder.setTitle("Set Holes");
builder.setItems(items, new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialogInterface, int item) {
Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
if (items[item] == "9"){
int setHoles = 9;
}
else if (items[item] == "18"){
int setHoles = 18;
}
return;
}
});
builder.create().show();
return;
Class that needs the data
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String name1 = EasyPar.helper.getNames(); <--- works fine for this one
int setHoles = EasyPar.helper.getHoles();
}
Whenever I try to use the value, it just comes up with nothing. If i try to:
textHoleNumber.setText(String.valueOf(setHoles));
it just displays 0
the only way I can get it to work is if i just set the value upfront as setHoles = 18; or so.
I tried to move the “helper” to the end of the class but that didnt work. I am thinking that the My Button is not correctly setting the setHoles value or I am not correctly passing the value outside of the “case R.id.buttonLetsGo” (if i put setHoles = 10; inside of public int getHoles() {
return setHoles;
} then it works no problem so its definitely calling that getHoles.
If theres any more data i can provide please let me know!
Thanks in advance!
Your problem seems to be a scoping problem:
You’re declaring new “setHoles” integers here, and they only exist for one line. Any other variable named “setHoles” from elsewhere in your code is being hidden by these new ones. (Your compiler should be giving you a warning about this, too.)
I hope that helps!