获取定制微调器的项目 [英] Getting item of customized Spinner

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

问题描述

我实现了自定义微调器:

I implemented custom spinner:

 public class MyAdapter extends ArrayAdapter<String>
        {

                public MyAdapter(Context context, int textViewResourceId,
                            String[] objects) {
                      super(context, textViewResourceId, objects);
                      // TODO Auto-generated constructor stub
                }
                @Override
            public View getDropDownView(int position, View convertView,ViewGroup parent) {
                return getCustomView(position, convertView, parent);
            }

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

            public View getCustomView(int position, View convertView, ViewGroup parent) {

                LayoutInflater inflater=getLayoutInflater();
                View row= inflater.inflate(R.layout.spinner, parent, false);
                TextView label=(TextView)row.findViewById(R.id.textView1);
                label.setText(data1[position]);

                ImageView icon=(ImageView)row.findViewById(R.id.imageView1);
                icon.setImageResource(images[position]);

                return row;
                }

       }

并设置为这样的微调器:

And set to a spinner like this:

spinner_choose_network_spinner.setAdapter(new MyAdapter(this, R.layout.spinner, data1));

我的问题是如何获取微调器当前所选项目的textview值?

My question is how do i get the textview value of the current selected item of my spinner?

推荐答案

Object selection = sp.getSelectedItem();

这将返回所选项目,您可以从中查询信息

this returns the selected item which you can query the info from

在您的适配器中,您需要实现getItem(int position)

in your adapter you need to implement getItem(int position)

并返回所需的项目,选择将为getItem(selectedPosition)

and return the needed item, selection will be getItem(selectedPosition)

假设您的getItem是

suppose your getItem is

public String getItem(int position){
return data1[position];

}

然后String selected = spinner.getSelectedItem();

将返回data1[selectedPosition]

为简单起见,我建议您创建一个新对象

For simplicity i would suggest you create a new object

public class MyCustomObject{

    String title;
    int imageResource;

}

然后您的数据数组将为

ArrayList<MyCustomObject>data = new ArrayList<MyCustomObject>();

以某种方式填充此数组

现在您的行将是

        public View getCustomView(int position, View convertView, ViewGroup parent) {

            LayoutInflater inflater=getLayoutInflater();
            View row= inflater.inflate(R.layout.spinner, parent, false);
            TextView label=(TextView)row.findViewById(R.id.textView1);
            label.setText(data.get(position).title);

            ImageView icon=(ImageView)row.findViewById(R.id.imageView1);
            icon.setImageResource(data.get(position).imageResource);

            return row;
            }

然后您的getItem将是

and then your getItem will be

public MyCustomObject getItem(int position){

return data.get(position);
}

然后您的选择将是

MyCustomObject selection = spinner.getSelectedItem();

,您会看到selection.title将可用,如果您需要selection.imageResource也将可用

and you can see selection.title will be available as well as selection.imageResource if you need it

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

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