Onitemselectedlistener
中选择数据时, spinner
不起作用。我不知道怎么了?
我选择的数据不可读。如何读取在spinner
中选择的数据?此版本的代码不起作用,所选数据不会出现。
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener, AdapterView.OnItemSelectedListener, View.OnTouchListener {
private Spinner spinner;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
spinner = (Spinner) findViewById(R.id.spinnerMain);
ArrayAdapter < String > adapterSpinner = new ArrayAdapter < String > (MainActivity.this,
android.R.layout.simple_spinner_item, Rooms);
adapterSpinner.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapterSpinner);
spinner.setOnItemSelectedListener(MainActivity.this);
adapterSpinner.notifyDataSetChanged();
final AdapterView.OnItemSelectedListener listener = new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView < ? > adapterView, View view, int i, long l) {
switch (i) {
case 0:
break;
case 1:
break;
case 2:
break;
}
}
@Override
public void onNothingSelected(AdapterView < ? > adapterView) {
}
};
final Spinner spnLocale = (Spinner) findViewById(R.id.spinnerMain);
spnLocale.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView < ? > adapterView, View view, int i, long l) {
Toast.makeText(MainActivity.this, "Selected", Toast.LENGTH_SHORT).show();
spnLocale.setOnItemSelectedListener(listener);
}
public void onNothingSelected(AdapterView < ? > adapterView) {
return;
}
});
// ...
}
// ...
}
答案 0 :(得分:0)
您已经将全局微调器置于顶部。像这样实现监听器-
spinner.setOnItemSelectedListener(new
AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(MainActivity.this, "Selected", Toast.LENGTH_SHORT).show();
spinner.setOnItemSelectedListener(listener);
}
public void onNothingSelected(AdapterView<?> adapterView) {
return;
}
});