Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9218839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:57:24+00:00 2026-06-18T02:57:24+00:00

I have a game which has a gold/silver/bronze award threshold for score. The game

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T02:57:26+00:00Added an answer on June 18, 2026 at 2:57 am
    private static int[] button_id{R.id.b1,R.id.b2.....};
    private void initButtons() {
        for(int i=0;i<button_id.length;i++){
            Button ONE_OF_YOUR_BUTTON=getButton(button_id[i]);
            ONE_OF_YOUR_BUTTON.doStuffToIt();
        }
    }
    
    
        }
    
    private static Button getButton(int id){
        return (Button)findViewById(id));
    }
    

    }

    You can always loop it through. And These kind of question belong in Code Review

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Java class called Game which has a non-static integer called score.
I have an XNA arcade game which runs over Silverlight environment. The game has
I have a game which has xml files in it. My game works on
In my game I have a base class of Loot which has methods universal
I have a core data class Game which has a to-many relationship to another
If I have a game which has a 3D world, and the world is
I have made a game which has a UISwitch that turns the music off.
I have an object named game which has a property called channels (which is
Let's say I have an entity called Game which has a home_school_id . I
I have an android game which loads a list of 'games' the user has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.