为spinner </string>设置自定义视图到ArrayAdapter <string>时出错

时间:2012-11-06 13:59:26

标签: android spinner android-arrayadapter

下面是我想要扩展视图的ArrayAdapter,以满足我对下拉微调器的需求。

    ArrayAdapter<String> arrShirtType = new ArrayAdapter<String>
        (this, android.R.layout.simple_spinner_item,shirtType)
        {
            private LayoutInflater inflater = LayoutInflater.from(getBaseContext());
            public View getView(int position, View convertView,ViewGroup parent) 
                {
                    View vi = convertView;
                    vi = inflater.inflate(R.layout.spinner_view_style, parent);
                    TextView txt = (TextView)findViewById(R.id.spinner_textview);
                    ((TextView) vi).setTypeface(font1);
                    ((TextView) vi).setTextSize(65);
                    return vi;
                }
            public View getDropDownView(int position, View convertView,ViewGroup parent) 
                {   
                    View vi = convertView;
                    vi = inflater.inflate(R.layout.spinner_view_style, parent);
                    TextView txt = (TextView)findViewById(R.id.spinner_textview);
                    ((TextView) vi).setTypeface(font1);
                    ((TextView) vi).setTextSize(65);
                    return vi;
                }
        };

问题已更新

我将视图扩展到ArrayAdapter的方式是否正确? 以下是错误日志

11-06 21:03:47.307: E/AndroidRuntime(1356): FATAL EXCEPTION: main
11-06 21:03:47.307: E/AndroidRuntime(1356): java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView
11-06 21:03:47.307: E/AndroidRuntime(1356):     at android.widget.AdapterView.addView(AdapterView.java:477)
11-06 21:03:47.307: E/AndroidRuntime(1356):     at android.view.LayoutInflater.inflate(LayoutInflater.java:497)
11-06 21:03:47.307: E/AndroidRuntime(1356):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
11-06 21:03:47.307: E/AndroidRuntime(1356):     at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
11-06 21:03:47.307: E/AndroidRuntime(1356):     at com.job.mommy.Shirt$1.getView(Shirt.java:90)
11-06 21:03:47.307: E/AndroidRuntime(1356):     

1 个答案:

答案 0 :(得分:2)

您必须初始化您的inflater。例如:

inflater = LayoutInflater.from(context);