在android应用中,我有2个活动。 MainActivity是篮球运动员(17位运动员)和PlayerDetailActivity的列表,其中包括他们的姓名,年龄,身高,位置和图像。如何减少if-else语句的使用?我知道最好使用所有属性创建Player类,但是我坚持了。
这是我的MainActivity代码
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);
}
}
这是我的PlayerDetailActivity代码
公共类PlayerDetailActivity扩展了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);
//播放器名称
String playerName = (String) getIntent().getExtras().get("playerName");
nameTextView.setText(playerName);
//球员年龄
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");
}
...
答案 0 :(得分:3)
您可以使用面向对象的方法完全替换if语句和switch语句。
这将使您可以使用以下代码,该代码更加干净。如果将来要使用数据源,它也更具动态性和可扩展性。
// In the PlayerDetailActivity
Player player = (Player) getIntent().getExtras().get("player");
nameTextView.setText(player.getName());
ageTextView.setText("Birth date: " + player.getBirthDate());
您需要在MainActivity中用ArrayList替换数组bostonCeltics。
// 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));
下面是如何制作Player类的示例。将其放在单独的文件中。
// 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;
}
}
为进一步了解对象和类,我建议使用此tutorial。如果您搜索“如何在Java中制作课程”,您还会找到其他人。
答案 1 :(得分:1)
最简单的直接建议是创建玩家姓名到生日的地图:
ageTextView = (TextView) findViewById(R.id.ageTextView);
String date = players.get(playerName) != null ? players.get(playerName) : "NA";
ageTextView.setText(date);
然后在您的活动中使用地图:
<input type="number" name="minutes" min="0" max="59">
<input type="number" name="seconds" min="0" max="59>
理想情况下,您不会对这么多数据进行硬编码。也许您可以从数据库或文件中加载。
答案 2 :(得分:-1)
您应该使用switch语句
switch (playerName){
case "Kyrie Irvin":
...
break;
case "Jayson Tatum":
...
}