Spinner setOnItemSelectedListener不触发

时间:2012-05-23 23:21:06

标签: android spinner

我有一个包含edittext,不可见微调器和按钮的视图对象。我通过LayoutInflater加载视图,它显示出来,一切正常。我按下按钮,微调器列表出现,我选择了我想要的项目。我的问题是,微调器的setOnItemSelectedListener不会触发,因此我无法将edittext设置为所选值。

ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item,listItems);                                     adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

                        Spinner spn = (Spinner)sEdit.findViewWithTag("spn"+fieldID);
                        final String spnHolder = "spn"+fieldID;

                        spn.setAdapter(adapter);
                        ImageButton bSpn = (ImageButton)sEdit.findViewWithTag("btn"+fieldID);
                        bSpn.setOnClickListener(new OnClickListener() {

                            @Override
                            public void onClick(View v) {
                                curVw = (EditText)sEdit.findViewWithTag(v.getTag().toString().split("btn")[1]);
                                ((Spinner)sEdit.findViewWithTag(spnHolder)).performClick();
                            }

                        });

                        try {
                            spn.setOnItemSelectedListener(new OnItemSelectedListener() {
                                @Override
                                public void onItemSelected(AdapterView<?> arg0, View arg1,
                                        int arg2, long arg3) {
                                    curVw.setText(((TextView)arg1).getText().toString());
                                }

                                @Override
                                public void onNothingSelected(AdapterView<?> arg0) {
                                    // TODO Auto-generated method stub

                                }
                            });
                        }
                        catch (Exception ex) 
                        {
                            ex.printStackTrace();
                        }

我添加了一个Try ... Catch,看看它是否只是失败了,但它永远不会被调用。

1 个答案:

答案 0 :(得分:0)

请使用:

,而不是使用SetOnclickListener
spn.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){

   //implements methods and perform your action. This will works definately
};