Koke Abeke
Koke Abeke

Reputation: 169

Optimize if-else statement android studio

In my Android app I have 2 activities. MainActivity is the list of basketball players (17 players) and PlayerDetailActivity with their name, age, height, position and images. How to reduce usage of if-else statement? I know that its better to create Player class with all its attributes, but I am stuck.

Here is my MainActivity code

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {

String[] bostonCeltics = new String[] {"Kyrie Irving", "Jayson Tatum", "Gordon Hayward", "Robert Williams", "Jabari Bird", "Al Horford", "Jaylen Brown", "Marcus Smart", "Terry Rozier", "Aron Baynes", "Marcus Morris", "Brad Wanamaker", "P.J.Dozier", "Daniel Theis", "Guerschon Yabusele", "Semi Ojeleye", "Walter Lemon, Jr"};

ListView listView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    listView = (ListView) findViewById(R.id.listView);

    ArrayAdapter<String> bostonCelticsAdapter = new ArrayAdapter<String>(getBaseContext(), android.R.layout.simple_list_item_1, bostonCeltics);

    listView.setAdapter(bostonCelticsAdapter);

    listView.setOnItemClickListener(this);

}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    Intent moveToDetailIntent = new Intent(getBaseContext(), PlayerDetailActivity.class);
    moveToDetailIntent.putExtra("playerName", bostonCeltics[position]);
    startActivity(moveToDetailIntent);
}

}

Here is my PlayerDetailActivity code:

public class PlayerDetailActivity extends AppCompatActivity {

    TextView nameTextView;
    TextView ageTextView;
    TextView heightTextView;
    TextView positionTextView;
    ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_player_detail);

        nameTextView = (TextView) findViewById(R.id.nameTextView);
        ageTextView = (TextView) findViewById(R.id.ageTextView);
        heightTextView = (TextView) findViewById(R.id.heightTextView);
        positionTextView = (TextView) findViewById(R.id.positionTextView);
        imageView = (ImageView) findViewById(R.id.imageView);

// Player name

        String playerName = (String) getIntent().getExtras().get("playerName");
        nameTextView.setText(playerName);

// Player age

        if (playerName.equals("Kyrie Irving")) {
            ageTextView.setText("Birth date: 23-Mar-92");
        }
        if (playerName.equals("Jayson Tatum")) {
            ageTextView.setText("Birth date: 3-Mar-98");
        }
        if (playerName.equals("Gordon Hayward")) {
            ageTextView.setText("Birth date: 23-Mar-90");
        }
...

Upvotes: 1

Views: 846

Answers (2)

J.A.P
J.A.P

Reputation: 715

You can replace the if-statements and switch-statements completely by using object-orientation.

That will allow you to use the following code, which is much cleaner. It's also more dynamic and expandable if you want to use a data source in the future.

// In the PlayerDetailActivity
Player player = (Player) getIntent().getExtras().get("player");
nameTextView.setText(player.getName());
ageTextView.setText("Birth date: " + player.getBirthDate());

You would need to replace the array bostonCeltics with an ArrayList, as following, in your MainActivity.

// Global variable
List<Player> bostonCeltics= new ArrayList<>();

// In your onCreate()
bostonCeltics.add(new Player("Kyrie Irving", "23-Mar-92"));
bostonCeltics.add(new Player("Jayson Tatum", "3-Mar-98"));
bostonCeltics.add(new Player("Gordon Hayward", "23-Mar-90"));

// When creating your intent
moveToDetailIntent.putExtra("player", bostonCeltics.get(position));

Below is an example of how you can make a Player class. Put it in a separate file.

// Implementing Serializable allows the object to be passed in intents between activities.
public class Player implements Serializable { 
    private String name;
    private String birthDate;

    // Constructor
    public Player(String name, String birthDate) {
        this.name = name;
        this.birthDate = birthDate;
    }

    // Below is just Getters and Setters
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getBirthDate() {
        return birthDate;
    }

    public void setBirthDate(String birthDate) {
        this.birthDate = birthDate;
    }
}

For further reading about objects and classes, I recommend this tutorial. You will find others if you search for "how to make a class in java".

Upvotes: 3

Tim Biegeleisen
Tim Biegeleisen

Reputation: 522646

The easiest immediate suggestion would be to create a map of player names to birthdates:

Map<String, String> players = new HashMap<>();
map.put("Kyrie Irving", "Birth date: 23-Mar-92");
map.put("Jayson Tatum", "Birth date: 3-Mar-98");
map.put("Gordon Hayward", "Birth date: 23-Mar-90");
// and so on

Then use the map in your activity:

ageTextView = (TextView) findViewById(R.id.ageTextView);
String date = players.get(playerName) != null ? players.get(playerName) : "NA";
ageTextView.setText(date);

Ideally, you would not be hard coding so much data. Perhaps you could load from a database or file.

Upvotes: 1

Related Questions