问题
public class MainActivity extends Activity implements OnClickListener, AdapterView.OnItemSelectedListener, OnCheckedChangeListener{
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
//Declare Variables to be used
final Bundle bundle = new Bundle();
final CheckBox checkbox01 = (CheckBox) findViewById(R.id.CheckBox01);
//All if statements for builds
final Button findbuilds = (Button) findViewById(R.id.btn_FindBuildsTab1);{
findbuilds.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if ((v==findbuilds)&&(checkbox01.getText()==("Ahri-AP"))&&((checkbox01).isChecked())){
final String charactername = "Ahri-AP";
}
if ((checkbox01).isChecked(){
//All Bundles are placed here
bundle.putString("CharacterName Value Key", charactername);
//Start Tabhost Activity and pass bundle(s)
Intent nextActivity = new Intent(v.getContext(),
nextActivity.putExtras(bundle);
startActivity(nextActivity); TabHostActivity.class);
}
Basically, what i am trying to do is create the string for the character's name in the first if-statement, and then bundle it in another if-statement. The reason i need to bundle the string(s) in a different if-statement is to avoid reaching the bytes limit of a method. I have a total of 64 different Strings that need to be bundled for each if-statement, and i have a total of 126 if-statements that need to happen. The problem is of course that the String variable is never found or resolved with the bundle in the 2nd if-statement. Is there a way to do this? Or maybe a more efficient way? Thanks in advance for the help! :)
回答1:
Just pull the definition of your variable outside of the if statement. eg:
final String charactername
if() {
charactername = ...
}
if() {
bundle stuff with charactername
}
来源:https://stackoverflow.com/questions/10901709/how-can-i-pass-a-string-value-that-has-been-created-in-an-if-statement-through