我在这个线程上读了很多类似的问题,但没有一个帮助我…
这是我的代码:
这是我的代码:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Spinner spinner = (Spinner) findViewById(R.id.imc_spinner); // Create an ArrayAdapter using the string array and a default spinner layout ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,R.array.imc_array,android.R.layout.simple_spinner_item); // Specify the layout to use when the list of choices appears adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // Apply the adapter to the spinner spinner.setAdapter(adapter); } public void onItemSelected(AdapterView<?> parent,View view,int pos,long id) { // An item was selected. You can retrieve the selected item using imc_met = parent.getItemAtPosition(pos).toString(); }
我声明imc_met为public String imc_met ;.问题是imc_met不包含微调框的选定项目的值,但它为空…
问题在哪里?
提前Thx.
解决方法
使用:
imc_met=Spinner.getSelectedItem().toString();
代替:
imc_met = parent.getItemAtPosition(pos).toString();
更新:
看来你将侦听器分配给你的微调器不正确的方式,做一些如下所示:
spin.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> arg0,View arg1,int arg2,long arg3) { String imc_met=spin.getSelectedItem().toString(); } @Override public void onNothingSelected(AdapterView<?> arg0) { // TODO Auto-generated method stub } });