gpt4 book ai didi

android - 选择新的微调项时从数组中删除值

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

我正在创建应用程序。我在其中通过微调器询问用户的血型,当用户选择血型时,我将血型添加到他可以献血的数组列表中。就像他选择血型 O-,我将添加所有血型排列为 O- 都可以向所有人献血。直到当用户错误地选择 O- 然后他选择 A- 时,它的工作正常问题才出现,所以现在我应该如何从 arrayList 中删除以前的组并添加组A-可以献血给谁现在我正在这样做:

    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
matchedGrpToDonate = spinner.getSelectedItem().toString();
matchedBloodGroups();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
private void matchedBloodGroups() {
if (matchedGrpToDonate.equals("A+")) {
bloodBankCanDonate.add("AB+");
bloodBankCanDonate.add("A+");
System.out.println("hash" + bloodBankCanDonate);
} else if (matchedGrpToDonate.equals("A+")) {
bloodBankCanDonate.remove("AB+");
bloodBankCanDonate.remove("A+");
System.out.println("hash" + bloodBankCanDonate);
}

if (matchedGrpToDonate.equals("A-")) {
bloodBankCanDonate.add("AB+");
bloodBankCanDonate.add("A+");
bloodBankCanDonate.add("AB-");
bloodBankCanDonate.add("A-");
System.out.println("hash" + bloodBankCanDonate);
} else if (matchedGrpToDonate.equals("A-")) {
bloodBankCanDonate.remove("AB+");
bloodBankCanDonate.remove("A+");
bloodBankCanDonate.remove("AB-");
bloodBankCanDonate.remove("A-");
System.out.println("hash" + bloodBankCanDonate);
}

if (matchedGrpToDonate.equals("B+")) {
bloodBankCanDonate.add("AB+");
bloodBankCanDonate.add("B+");
System.out.println("hash" + bloodBankCanDonate);
} else if (matchedGrpToDonate.equals("B+")) {
bloodBankCanDonate.remove("AB+");
bloodBankCanDonate.remove("B+");
System.out.println("hash" + bloodBankCanDonate);
}

if (matchedGrpToDonate.equals("B-")) {
bloodBankCanDonate.add("AB+");
bloodBankCanDonate.add("B+");
bloodBankCanDonate.add("AB-");
bloodBankCanDonate.add("B-");
System.out.println("hash" + bloodBankCanDonate);
} else if (matchedGrpToDonate.equals("B-")) {
bloodBankCanDonate.remove("AB+");
bloodBankCanDonate.remove("B+");
bloodBankCanDonate.remove("AB-");
bloodBankCanDonate.remove("B-");
System.out.println("hash" + bloodBankCanDonate);
}

if (matchedGrpToDonate.equals("O+")) {
bloodBankCanDonate.add("AB+");
bloodBankCanDonate.add("A+");
bloodBankCanDonate.add("B+");
bloodBankCanDonate.add("O+");
System.out.println("hash" + bloodBankCanDonate);

} else if (matchedGrpToDonate.equals("O+")) {
bloodBankCanDonate.remove("AB+");
bloodBankCanDonate.remove("A+");
bloodBankCanDonate.remove("B+");
bloodBankCanDonate.remove("O+");
System.out.println("hash" + bloodBankCanDonate);
}

if (matchedGrpToDonate.equals("O-")) {
bloodBankCanDonate.add("AB+");
bloodBankCanDonate.add("A+");
bloodBankCanDonate.add("B+");
bloodBankCanDonate.add("O+");
bloodBankCanDonate.add("AB-");
bloodBankCanDonate.add("A-");
bloodBankCanDonate.add("B-");
bloodBankCanDonate.add("O-");
System.out.println("hash" + bloodBankCanDonate);
} else if (matchedGrpToDonate.equals("O-")) {
bloodBankCanDonate.remove("AB+");
bloodBankCanDonate.remove("A+");
bloodBankCanDonate.remove("B+");
bloodBankCanDonate.remove("O+");
bloodBankCanDonate.remove("AB-");
bloodBankCanDonate.remove("A-");
bloodBankCanDonate.remove("B-");
bloodBankCanDonate.remove("O-");
System.out.println("hash" + bloodBankCanDonate);
}
}

最佳答案

您可以在 if 语句中添加一个检查,如果数组列表不为空,则清除它并像这样添加血型值:

private void matchedBloodGroups() {
if (matchedGrpToDonate.equals("A+")) {
if (!bloodBankCanDonate.isEmpty())
bloodBankCanDonate.clear();
bloodBankCanDonate.add("AB+");
bloodBankCanDonate.add("A+");
System.out.println("hash" + bloodBankCanDonate);
}
if (matchedGrpToDonate.equals("A-")) {
if (!bloodBankCanDonate.isEmpty())
bloodBankCanDonate.clear();
bloodBankCanDonate.add("AB+");
bloodBankCanDonate.add("A+");
bloodBankCanDonate.add("AB-");
bloodBankCanDonate.add("A-");
System.out.println("hash" + bloodBankCanDonate);
}

您可以对其他血型进行同样的操作,但请记住,此条件仅适用于您选择可以献血的血型。

关于android - 选择新的微调项时从数组中删除值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59405678/

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