1

在我的应用程序中,我必须使用一个应该显示三个项目(IT、EN、PR)的微调器。我还必须使用个性化的布局。该代码位于 onCreate 方法的另一个适配器中:

holder.spinnerLenguage = (Spinner) convertView
                .findViewById(R.id.spinnerLenguage);
        ArrayList<String> spinnerArray = new ArrayList<String>();
        spinnerArray.add("IT");
        spinnerArray.add("EN");
        spinnerArray.add("PR");
        final lenguageSpinnerAdapter lenguageAdapter = new lenguageSpinnerAdapter(
                convertView.getContext(), spinnerArray);
        holder.spinnerLenguage.setAdapter(lenguageAdapter);

        holder.spinnerLenguage
                .setOnItemSelectedListener(new OnItemSelectedListener()
...
...

适配器类是:

private class lenguageSpinnerAdapter extends ArrayAdapter<String> implements
        SpinnerAdapter {

    private ArrayList<String> lenguages;

    public lenguageSpinnerAdapter(Context context,
            ArrayList<String> lenguages) {
        super(context, R.layout.lenguage_item_layout);
        this.lenguages = lenguages;
    }

    @Override
    public View getDropDownView(int position, View convertView,
            ViewGroup parent) {
        // First let's verify the convertView is not null
        if (convertView == null) {
            // This a new view we inflate the new layout
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.lenguage_item_layout,
                    parent, false);
        }

        TextView lenguage = (TextView) convertView
                .findViewById(R.id.textViewLenguage);
        lenguage.setText(lenguages.get(position).toString());

        return convertView;

    }

}

编辑:使用 getCount 错误是:

07-11 11:09:20.222: E/AndroidRuntime(32564): FATAL EXCEPTION: main
07-11 11:09:20.222: E/AndroidRuntime(32564): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app/com.app.FlipMainActivity}: java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
07-11 11:09:20.222: E/AndroidRuntime(32564):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2247)
07-11 11:09:20.222: E/AndroidRuntime(32564):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2297)
07-11 11:09:20.222: E/AndroidRuntime(32564):    at android.app.ActivityThread.access$700(ActivityThread.java:152)
07-11 11:09:20.222: E/AndroidRuntime(32564):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1282)
07-11 11:09:20.222: E/AndroidRuntime(32564):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-11 11:09:20.222: E/AndroidRuntime(32564):    at android.os.Looper.loop(Looper.java:137)
07-11 11:09:20.222: E/AndroidRuntime(32564):    at android.app.ActivityThread.main(ActivityThread.java:5328)
07-11 11:09:20.222: E/AndroidRuntime(32564):    at java.lang.reflect.Method.invokeNative(Native Method)
07-11 11:09:20.222: E/AndroidRuntime(32564):    at java.lang.reflect.Method.invoke(Method.java:511)
07-11 11:09:20.222: E/AndroidRuntime(32564):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
07-11 11:09:20.222: E/AndroidRuntime(32564):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
07-11 11:09:20.222: E/AndroidRuntime(32564):    at dalvik.system.NativeStart.main(Native Method)
07-11 11:09:20.222: E/AndroidRuntime(32564): Caused by: java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
07-11 11:09:20.222: E/AndroidRuntime(32564):    at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:386)
07-11 11:09:20.222: E/AndroidRuntime(32564):    at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
07-11 11:09:20.222: E/AndroidRuntime(32564):    
07-11 11:09:20.222: E/AndroidRuntime(32564):    at android.widget.Spinner.onMeasure(Spinner.java:443)
07-11 11:09:20.222: E/AndroidRuntime(32564):    at android.view.View.measure(View.java:16059)
071 11:09:20.222: E/AndroidRuntime(32564):  at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:681)
07-11 11:09:20.222: E/AndroidRuntime(32564):    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
07-11 11:09:20.222: E/AndroidRuntime(32564):    at android.view.View.measure(View.java:16059)
07-11 11:09:20.222: E/AndroidRuntime(32564):    at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:681)
...
...
07-11 11:09:20.222: E/AndroidRuntime(32564): Caused by: java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.TextView
07-11 11:09:20.222: E/AndroidRuntime(32564):    at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:379)
07-11 11:09:20.222: E/AndroidRuntime(32564):    ... 29 more

微调器在布局中绘制,但没有项目。有任何想法吗?

4

2 回答 2

1

尝试实现以下代码而不是您的代码。

private class lenguageSpinnerAdapter extends BaseAdapter implements
        SpinnerAdapter {

    private ArrayList<String> lenguages;

    public lenguageSpinnerAdapter(Context context,
            ArrayList<String> lenguages) {
        this.lenguages = lenguages;
    }

    public int getCount() {
        return lenguages.size();
    }

    public Object getItem(int position) {
        return lenguages.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            // This a new view we inflate the new layout
            LayoutInflater inflater = (LayoutInflater) getApplicationContext()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.get_exercise_activity,
                    parent, false);
        }

        TextView lenguage = (TextView) convertView
                .findViewById(R.id.btnSave);
        lenguage.setText(lenguages.get(position).toString());

        return convertView;
    }

}
于 2013-07-11T08:58:14.387 回答
0

请参阅Android Spinner Dropdown Example教程,这将对您有所帮助。

于 2013-07-11T09:10:12.247 回答