如何获得微调控件选择的id

时间:2012-11-09 06:41:00

标签: android

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

在课堂上

,我想获得所选的微调控件的id。 我怎样才能做到这一点 ?

我已经使用过这段代码,但是当我运行它时,它表示存在问题并且已关闭。

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

我尝试了getSelectedItemId和getSelectedItemPosition,但没有使用它们。

2 个答案:

答案 0 :(得分: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();

答案 1 :(得分: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.