gpt4 book ai didi

java - 优化if-else语句android studio

转载 作者:行者123 更新时间:2023-11-29 04:13:26 25 4
gpt4 key购买 nike

在我的 Android 应用程序中,我有 2 个 Activity 。 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 代码:

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");
}
...

最佳答案

您可以使用面向对象完全替换 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 中制作类”,您会找到其他人。

关于java - 优化if-else语句android studio,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53735042/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com