自定义微调器适配器上的setOnItemSelectedListener不起作用

时间:2015-01-14 22:51:49

标签: android spinner onitemselectedlistener

大家好,希望你能帮助我,我为我的微调器设置了自定义ArrayAdapter,并在onPostExecute()内将适配器设置在AsyncTask内。问题是当我调用setOnItemSelectedListener()时它无法识别click事件。我的微调器就像一个对话框,它工作正常,项目列表显示在弹出式微调器中,但项目选择器不起作用..我想这是因为我错过了setOnItemSelectedListener()内的上下文,但不确定。

这是我的代码:

onPostExecute

protected void onPostExecute(String result){
                pDialog.dismiss(); 

spinAdmin.setAdapter(new MyAdapter(getApplication(), R.layout.opina_spinner_item_saf,safList));

                spinAdmin.setOnItemSelectedListener(new OnItemSelectedListener(){

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

                        Toast.makeText(getBaseContext(),"MyItem", 
                                Toast.LENGTH_SHORT).show();
                    }

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

                });
            }

在我的适配器类Activity中:

public class MyAdapter extends ArrayAdapter<String>{
        public MyAdapter(Context ctx, int txtViewResourceId, ArrayList<String> objects) { 
            super(ctx, txtViewResourceId, objects);
        }

        @Override 
        public View getDropDownView(int position, View cnvtView, ViewGroup prnt) {
            return getCustomView(position, cnvtView, prnt); 
            }
        @Override 
        public View getView(int position, View cnvtView, ViewGroup prnt) { 
            return getCustomView(position, cnvtView, prnt); 
            }

        public View getCustomView(int position, View cnvtView, ViewGroup prnt) { 
            LayoutInflater inflater = getLayoutInflater(); 
            View mySpinner = inflater.inflate(R.layout.opina_spinner_item_saf, prnt, false); 

            TextView main_text = (TextView) mySpinner .findViewById(R.id.txt_spinner_item_saf); 
            main_text.setText(safList.get(position)); 

            return mySpinner;

            }

    }

1 个答案:

答案 0 :(得分:1)

试试这个:

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

        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });