I have a game which has a gold/silver/bronze award threshold for score. The game saves the award achieved in a SharedPreferences file with the key being the level number. So on my level select screen, I have 20 levels. My code looks like this:
private void initButtons() {
Button b1 = ((Button) findViewById(R.id.b1));
load = getSharedPreferences(Game.DATA, Game.GOLD);
if(load.getInt(Integer.toString(1), 0) == 4){
b1.setBackgroundDrawable(getResources().getDrawable(
R.drawable.gold));
b1.setText("1");
b1.setTextSize(30);
b1.setOnClickListener(this);
}
((Button) findViewById(R.id.b2)).setOnClickListener(this);
((Button) findViewById(R.id.b3)).setOnClickListener(this);
((Button) findViewById(R.id.b4)).setOnClickListener(this);
((Button) findViewById(R.id.b5)).setOnClickListener(this);
((Button) findViewById(R.id.b6)).setOnClickListener(this);
((Button) findViewById(R.id.b7)).setOnClickListener(this);
((Button) findViewById(R.id.b8)).setOnClickListener(this);
((Button) findViewById(R.id.b9)).setOnClickListener(this);
((Button) findViewById(R.id.b10)).setOnClickListener(this);
((Button) findViewById(R.id.b11)).setOnClickListener(this);
((Button) findViewById(R.id.b12)).setOnClickListener(this);
((Button) findViewById(R.id.b13)).setOnClickListener(this);
((Button) findViewById(R.id.b14)).setOnClickListener(this);
((Button) findViewById(R.id.b15)).setOnClickListener(this);
((Button) findViewById(R.id.b16)).setOnClickListener(this);
((Button) findViewById(R.id.b17)).setOnClickListener(this);
((Button) findViewById(R.id.b18)).setOnClickListener(this);
((Button) findViewById(R.id.b19)).setOnClickListener(this);
((Button) findViewById(R.id.b20)).setOnClickListener(this);
}
So I would need 3 if, else if statements for each button. That seems like a lot. Is the above code efficient or should I use another method?
Thanks for the help
Andy
}
You can always loop it through. And These kind of question belong in Code Review