自定义微调器适配器simple_spinner_dropdown_item无法正确显示

时间:2013-09-02 07:42:59

标签: android android-layout android-spinner

我从SQLite数据库获取数据并将它们分配给数据对象。我正在使用自定义微调器适配器(实现SpinnerAdapter)将它们放在微调器中,以覆盖getItem()getItemId()方法。但是,android.R.layout.simple_spinner_dropdown_item无法正确显示(来自我的Galaxy Tab 2 10.1的屏幕截图):

Wrong spinner from data object

相比之下,这是一个由R.array中的数组填充的微调器(正确的外观/大小):

Proper spinner from R.array

完成此代码:

ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(), R.array.quote_prices, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);

我无法通过实现/扩展某种适配器来获得这种外观(我也试过扩展BaseAdapter无效)。这是我的微调器适配器的代码:

public class MySpinnerAdapter implements SpinnerAdapter {

    private Context context;
    /**
     * The internal data (the ArrayList with the Objects).
     */
    private List<? extends BaseDO> data;

    public MySpinnerAdapter(Context context, List<?extends BaseDO> data){
        this.context = context;
        this.data = data;
    }

    /**
     * Returns the Size of the ArrayList
     */
    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return data.get(position).getId();
    }

    @Override
    public int getItemViewType(int position) {
        return 0;
    }

    @Override
    public int getViewTypeCount() {
        return 1;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public boolean isEmpty() {
        return false;
    }

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = vi.inflate(android.R.layout.simple_spinner_dropdown_item, null);
        }
        ((TextView) convertView).setText(data.get(position).toString());
        return convertView;

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        TextView textView = (TextView) View.inflate(context, android.R.layout.simple_spinner_item, null);
        textView.setText(data.get(position).toString());
        return textView;
    }


    @Override
    public void registerDataSetObserver(DataSetObserver observer) {

    }

    @Override
    public void unregisterDataSetObserver(DataSetObserver observer) {

    }

    public List<? extends BaseDO> getData() {
        return data;
    }

}

任何帮助都将不胜感激。

编辑:我忘了提到这是用

调用的
spnnrTerm.setAdapter(new MySpinnerAdapter(getActivity(), termList));

1 个答案:

答案 0 :(得分:14)

我知道这个问题已经过时但我遇到了同样的问题并且刚刚发现了问题。在检查ArrayAdapter.java的来源之后,我注意到它正在将ViewGroup传递给inflater而我却没有。这一直是个问题。

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = vi.inflate(android.R.layout.simple_spinner_dropdown_item, parent, false);
        }
        ((TextView) convertView).setText(data.get(position).toString());
        return convertView;

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        TextView textView = (TextView) View.inflate(context, android.R.layout.simple_spinner_item, null);
        textView.setText(data.get(position).toString());
        return textView;
    }