我正在尝试在android studio上制作一个简单的应用程序,这将允许我选择两个团队(来自旋转器),然后打印出他们的最终分数或者没有玩。
我已经能够完成所有设置和工作,但是我无法在textView中显示得分。它总是说团队没有玩
`public class MainActivity extends AppCompatActivity {
Spinner spinner1, spinner2;
ArrayAdapter<CharSequence> adapter1, adapter2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner1 = (Spinner) findViewById(R.id.spinner1);
adapter1 = ArrayAdapter.createFromResource(this,R.array.list1,android.R.layout.simple_spinner_item);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter1);
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
TextView spinner1Text = (TextView) view;
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
spinner2 = (Spinner) findViewById(R.id.spinner2);
adapter2 = ArrayAdapter.createFromResource(this,R.array.list2,android.R.layout.simple_spinner_item);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(adapter2);
spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
TextView spinner1Text = (TextView) view;
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
final Button button = (Button)findViewById(R.id.Show);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
button.refreshDrawableState();
}
});
TextView score = (TextView) findViewById(R.id.Score);
if (spinner1.getSelectedItem().toString().equals("Patriots")&& spinner2.getSelectedItem().toString().equals("Seahawks")) {
score.setText("Patriots 21, Seahawks 14" + "\n" + "Q1 Patroits 3 Seahawks 0" + "\n" + "Q2 Patroits 3 Seahawks 14" + "\n" + "Q3 Patroits 14 Seahawks 14" + "\n" + "Q4 Patroits 21 Seahawks 14");}
else if (spinner1.getSelectedItem().toString().equals("Falcons")&& spinner2.getSelectedItem().toString().equals("Raiders")){
score.setText("Falcons 3, Raiders 18" + "\n" + "Q1 Falcons 0 Raider 0" + "\n" + "Q2 Falcons 0 Raider 13" + "\n" + "Q3 Falcons 0 Raider 15" + "\n" + "Q4 Falcons 0 Raider 18");}
else if (spinner1.getSelectedItem().toString().equals("Eagles")&& spinner2.getSelectedItem().toString().equals("Cowboys")){
score.setText("Eagles 42, Cowboys 38" + "\n" + "Q1 Eagles 3 Cowboys 21" + "\n" + "Q2 Eagles 21 Cowboys 29" + "\n" + "Q3 Eagles 28 Cowboys 35" + "\n" + "Q4 Eagles 42 Cowboys 38");}
else if (spinner1.getSelectedItem().toString().equals("Bears")&& spinner2.getSelectedItem().toString().equals("Redskins")){
score.setText("Bears 20, Redskins 0" + "\n" + "Q1 Bears 3 Redskins 0" + "\n" + "Q2 Bears 3 Redskins 0" + "\n" + "Q3 Bears 17 Redskins 0" + "\n" + "Q4 Bears 20 Redskins 0");}
else score.setText("Those two teams did not play each other");
}
<resources>
<string name="app_name">NFL Scorer Application</string>
<string name="first_team">Please select the first team</string>
<string name="second_team">Please select the second team</string>
<string-array name="list1">
<item></item>
<item>Patriots</item>
<item>Falcons</item>
<item>Eagles</item>
<item>Bears</item>
</string-array>
<string-array name="list2">
<item></item>
<item>Seahawks</item>
<item>Raiders</item>
<item>Cowboys</item>
<item>Redskins</item>
</string-array>
</resources>
答案 0 :(得分:1)
感谢Pavneet Singh,我只需要将if-else移动到监听器