为此提供了许多旧的答案,但没有人帮助过,在进行这项研究之前已花了2周时间进行研究。我有两个通过strings.xml创建的微调器,当用户从每个微调器中选择一个选项时,它会将textView更改为特定值,例如: 3.下面是我根据网上的例子放在一起的,问题似乎是.equals和setText返回一个无法解决的方法错误。
可以提供任何可以提供的帮助。
public class Main3Activity extends AppCompatActivity implements OnItemSelectedListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
Spinner spinner = (Spinner) findViewById(R.id.spinner);
spinner.setOnItemSelectedListener(this);
Spinner spinner2 = (Spinner) findViewById(R.id.spinner2);
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (spinner.equals("Selection 1")&& spinner2.equals("Option 2"))
textView8.setText("3");
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
IntelliJ答案解决了我的问题,如下所示。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
Spinner spinner = (Spinner) findViewById(R.id.spinner);
Spinner spinner2 = (Spinner) findViewById(R.id.spinner2);
TextView textView8=(TextView) findViewById(R.id.textView8);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (spinner.getSelectedItem().toString().equals("Selection 1")
&& spinner2.getSelectedItem().toString().equals("Option 2"))
{
textView8.setText("3");
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}
答案 0 :(得分:3)
您应该添加 getSelectedItem().toString()
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
Spinner spinner = (Spinner) findViewById(R.id.spinner);
Spinner spinner2 = (Spinner) findViewById(R.id.spinner2);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (spinner.getSelectedItem().toString().equals("Selection 1"))
{
textView8.setText("3");
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (spinner2.getSelectedItem().toString().equals("Option 2"))
{
textView8.setText("3");
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}