在ArrayAdapter风格问题,压倒一切的getDropDownView时 [英] Style issue when overriding getDropDownView in ArrayAdapter

查看:1646
本文介绍了在ArrayAdapter风格问题,压倒一切的getDropDownView时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一个奇怪的现象,当覆盖的 ArrayAdapter 的getDropDownView 方式。我需要为了从我的自定义对象显示正确的字符串值了重写此方法。那怎么我的阵列适配器的样子:

I'm experiencing a strange behavior when overriding getDropDownView method of ArrayAdapter. I need to override this method in order to show correct string value out from my custom object. Thats how my array adapter look like:

ArrayAdapter<NoteType> adapter = new ArrayAdapter<NoteType>(this, android.R.layout.simple_spinner_item, lstNoteTypes){
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        TextView lbl = (TextView) super.getView(position, convertView, parent);
        lbl.setText(getItem(position).getName());
        return lbl;
    }

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        TextView lbl = (TextView) super.getView(position, convertView, parent);
        lbl.setText(getItem(position).getName());
        return lbl;
    }
};
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

所以,当我重写 getDropDownView ,我的微调看起来如下 - 该项目的高度是非常小的尺寸和这不是我想要的:

So when I override getDropDownView, my Spinner looks as below - the item height is very small in size and which is not what I want:

但是,当我评论(或没有覆盖)的 getDropDownView 方法,那么它看起来很好的默认样式,但后来我不能注入所需的文本值成下拉项。

But when I comment (or don't override) the getDropDownView method then it looks fine with the default styling but then I can't inject the required text value into drop down items.

请注意项目的高度,两个图像只是因为会覆盖 getDropDownView

Note the height of items in both images just because of overriding getDropDownView.

有什么建议?或者我是什么在我的code失踪?

Any suggestions? or what am I missing in my code?

推荐答案

我觉得我的也许的知道为什么会这样,但我不得不测试它,所以现在这里是另外一个(快)解决方案。

I think I might know why this happens, but I have to test it, so for now here is another (quick) solution.

由于微调电话的toString() ArrayAdapter ,你可以覆盖的toString()在 NoteType 方法,让它返回您的字符串(而不是默认的的toString()实施)。这样,您就不必重写 getDropDownView()在您的适配器,但仍然有默认的样式,并在其中的数据。

Since the Spinner calls toString() on every object in the ArrayAdapter, you could override the toString() method in your NoteType class and let it return your String (instead of the default toString() implementation). This way you wouldn't have to override getDropDownView() in your adapter, but still have the default styling and your data in it.

这篇关于在ArrayAdapter风格问题,压倒一切的getDropDownView时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆