定制setDropDownViewResource微调项示例 [英] Example of custom setDropDownViewResource spinner item

查看:261
本文介绍了定制setDropDownViewResource微调项示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在下拉查看我的飞旋的显示两个值。

I would like to display two values in an drop down view of my spinner.

目前,它只有一个城市的名字,但我也想小的距离场添加到它。

Currently, it only has a city name, but I would also like to add a small distance field to it.

    MyCity<MyCityDistance> dataAdapter;  

    dataAdapter = new MyCity(this, R.layout.mycityrow, list);
    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

我把所有的code自定义数据适配器,exapanding我的观点和支架等。

I have all the code for custom data adapter, exapanding my view and holder etc.

不过,它得到展示的项目不同时显示的城市,从我的当前位置的距离。

However, the item which gets show doesn't display both the city and its distance from my current location.

这只能说明什么是MyCityDistance类的toString()方法重写。

It only shows what is overridden in toString() method of MyCityDistance class.

我甚至尝试设置 dataAdapter.setDropDownViewResource(R.layout.mycityrow);

I even tried setting dataAdapter.setDropDownViewResource(R.layout.mycityrow);

但是,没有成功。它抛出一个错误。

but, no success. It throws an error.

11月4日至2号:05:22.600:E / AndroidRuntime(367):java.lang.IllegalStateException:ArrayAdapter需要的资源ID是一个TextView 11月4日至2号:05:22.600:E / AndroidRuntime(367):在android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:347) 11月4日至2号:05:22.600:E / AndroidRuntime(367):在android.widget.ArrayAdapter.getDropDownView(ArrayAdapter.java:376) 11月4日至2号:05:22.600:E / AndroidRuntime(367):在android.widget.Spinner $ DropDownAdapter.getDropDownView(Spinner.java:332)

04-02 11:05:22.600: E/AndroidRuntime(367): java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView 04-02 11:05:22.600: E/AndroidRuntime(367): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:347) 04-02 11:05:22.600: E/AndroidRuntime(367): at android.widget.ArrayAdapter.getDropDownView(ArrayAdapter.java:376) 04-02 11:05:22.600: E/AndroidRuntime(367): at android.widget.Spinner$DropDownAdapter.getDropDownView(Spinner.java:332)

什么是用户自己创建setDropDownViewResource的一个很好的例子()。

What is a good example of creating your own custom setDropDownViewResource().

就算我注释掉setDropDownViewResource()行,我得到了同样的错误。

Even if I comment out the setDropDownViewResource() line, I get the same error.

注:仅影响mycityrow电流是微调的第一个元素是显示为每mycityrow的布局。但是,当我点击向下打开下拉列表,该布局将丢失。我想下拉选项过于期间相同的布局。

Note: The only effect mycityrow current is that the first element of Spinner is show as per the layout of mycityrow. However, when I click open the drop down, that layout is lost. I want the same layout during drop down selection too.

推荐答案

请注意下面的示例使用内置的 android.R.layout.simple_list_item_2 ,不幸的是,文本颜色会可能是相同的背景。你可以简单地解决这个问题通过创建自己的自定义视图,并把它用在适配器代替。

Note the below example uses the inbuilt android.R.layout.simple_list_item_2, Unfortunately the text color will probably be the same as the background. You can simply solve this by creating your own custom view and use it in the adapter instead.

让我知道,如果我要解释它的任何部分。

Let me know if i should explain any part of it.

public class MainActivity extends Activity {

    class City {
        public City(String city, int d) {
            this.city = city;
            this.distance = String.valueOf(d);
        }

        String city;
        String distance;
    }

    class CityAdapter extends ArrayAdapter<City> {

        public CityAdapter(Context context, List<City> objects) {
            super(context, android.R.layout.simple_list_item_2, objects);
        }

        @Override //don't override if you don't want the default spinner to be a two line view
        public View getView(int position, View convertView, ViewGroup parent) {
            return initView(position, convertView);
        }

        @Override
        public View getDropDownView(int position, View convertView,
                                    ViewGroup parent) {
            return initView(position, convertView);
        }

        private View initView(int position, View convertView) {
            if(convertView == null)
                convertView = View.inflate(getContext(),
                                           android.R.layout.simple_list_item_2,
                                           null);
            TextView tvText1 = (TextView)convertView.findViewById(android.R.id.text1);
            TextView tvText2 = (TextView)convertView.findViewById(android.R.id.text2);
            tvText1.setText(getItem(position).city);
            tvText2.setText(getItem(position).distance);
            return convertView;
        }
    }

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Spinner spinner = (Spinner)findViewById(R.id.spinner1);
        List<City> list = new ArrayList<MainActivity.City>();
        for(int i = 0; i < 10; i++)
            list.add(new City(String.format("City %d", i + 1), (i + 1) * 1000));
        spinner.setAdapter(new CityAdapter(this, list));

    }

}

这篇关于定制setDropDownViewResource微调项示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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