0

我有一个微调控件,我通过数据适配器连接它并通过它获取数据。

在课堂上,我想获取所选微调器控件的 id 。我怎样才能做到这一点 ?

我已经使用了这段代码,但是当我运行它时,它说有问题并且它关闭了。

Toast toast=Toast.makeText(MainActivity.this,sp.getSelectedItemId(),5000);
toast.setGravity(Gravity.CENTER,100, 0);
toast.show(); 

我尝试了 getSelectedItemId 和 getSelectedItemPosition 但它们都不起作用。

4

2 回答 2

0

这里是你问题的答案

ArrayAdapter<String> adpt = new ArrayAdapter<String>this,android.R.layout.simple_spinner_item, strType);
adpt.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spnType.setAdapter(adpt);
spnType.setOnItemSelectedListener(new OnItemSelectedListener()
 {
  public void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3)
  {
    int intItemType = spnType.getSelectedItemPosition();
  }
  public void onNothingSelected(AdapterView<?> arg0) 
  {

  }
});

这是获取微调器的选定位置的行

    int intItemType = spnType.getSelectedItemPosition();
于 2012-11-09T06:45:26.527 回答
0
    Try this

            //Spinner OnItemClick Event here
            payfeeTabStudentNameSpinner.setOnItemSelectedListener(new OnItemSelectedListener(){

                @Override
                public void onItemSelected(AdapterView<?> parent, View view,
                        int position, long id) {

                String spinnerSelectedValue = parent.getItemAtPosition(position).toString();
                }
                @Override
                public void onNothingSelected(AdapterView<?> arg0) {
                    // TODO Auto-generated method stub
                }
            });

Please let me know your problem is resolved or not.
于 2012-11-09T06:47:14.713 回答