如何获取动态创建的微调器android java的选定值

时间:2014-12-29 14:37:27

标签: java android spinner

在我的Android应用程序中,我动态创建了微调器,我希望获得动态创建的微调器的选定值,我的工作如下:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_answermultitext);

String Title = "";
        for (int count = 0; count < DropDown.length; count++) {
            Title = DropDown[count].split(":")[0];
            String DValue = DropDown[count].split(":")[1];
            for (int i = 0; i < arr.length; i++) {
                if (DValue.contains(arr[i])) {
                    DValue = DValue.replace(arr[i], "");
                }
                if(Title.contains(arr[i]))
                {
                    Title = Title.replace(arr[i], "");
                }
            }
            String[] data = DValue.split(",");

            ///Creating Text View
            tv[count] = new TextView(this);
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                    (int) LayoutParams.WRAP_CONTENT,
                    (int) LayoutParams.WRAP_CONTENT);
            params.leftMargin = 25;
            params.topMargin = (count + 1) * (75 + add);
            tv[count].setText(Title);
            tv[count].setTextSize((float) 20);
            tv[count].setPadding(5, 15, 5, 15);
            tv[count].setLayoutParams(params);
            layout.addView(tv[count]);

            ///Creating Spinner
            Sp[count] = new Spinner(this);
            RelativeLayout.LayoutParams param = new RelativeLayout.LayoutParams(
                    (int) LayoutParams.WRAP_CONTENT,
                    (int) LayoutParams.WRAP_CONTENT);
            param.leftMargin = 25;
            param.topMargin = (count + 1) * (100 + add1);
            Sp[count].setPadding(5, 23, 5, 5);
            Sp[count].setLayoutParams(param);
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                    android.R.layout.simple_spinner_item, data);
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            Sp[count].setAdapter(adapter);
            Sp[count].setOnItemSelectedListener(new OnItemSelectedListener() {

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

                        int sid = Sp[count].getSelectedItemPosition();
                        // spinnerDropDown.getSelectedItem().toString();
                        Toast.makeText(getBaseContext(),
                                "You have selected : " + DropDown[sid],
                                Toast.LENGTH_SHORT).show();
                }

                // drop_SelectedVal
                @Override
                public void onNothingSelected(AdapterView<?> parent) {
                    // TODO Auto-generated method stub
                }
            });
            layout.addView(Sp[count]);
        }
}

但是我在此行onItemSelected()

int sid = Sp[count].getSelectedItemPosition();方法中遇到错误
Cannot refer to a non-final variable count inside an inner class defined in a different method

当我使用final vaiable添加count时,会删除上述错误,并且我在此行for(int count = 0; count < DropDown.length; count++)

以下错误
The final local variable count cannot be assigned. It must be blank and not using a compound assignment

请建议我如何解决它。

等待回复。

由于

2 个答案:

答案 0 :(得分:1)

你可以使用

int position
onItemSelected上的

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

答案 1 :(得分:0)

首先关于错误:错误是因为您正在访问内部类中的非最终Spinner对象Sp [count]。因此将计数更改为最终将产生另一个错误,但之前的问题仍未解决。它只是暂时被压制,当你用计数来解决错误时它会再次出现。

如何解决?

首先删除您在计数中添加的最终结果。

第二如果您只需要微调器中所选项目的位置,则应执行以下操作

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

第三如果要选择字符串,则应执行以下操作:

@Override
  public void onItemSelected(AdapterView<?> parent, View view,int position, long id) {
   String s = (String) parent.getItemAtPosition(position);
   // here's the change
  }