添加到OnItemSelectedListener微调 [英] Adding OnItemSelectedListener to Spinner

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

问题描述

我有一个按钮和一个微调(最初隐藏)。当用户presses一个按钮,微调被填充的项目和变得可见。现在,我想OnItemSelectedListener添加到微调。我试过很多教程,没有运气。

I have a button and a spinner (originally hidden). When user presses a button, spinner gets populated with items and becomes visible. Now I would like to add OnItemSelectedListener to the spinner. and I have tried many tutorials with no luck.

这是我的OnCreate函数

This is my OnCreate function

public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Button load_routes = (Button)findViewById(R.id.load_routes);
    Spinner routes = (Spinner)findViewById(R.id.routes_list);

    load_routes.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View v)
        {
            load_routes(v);
        }
    });

    routes.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
    {
        public void onItemSelected(AdapterView<?> arg0, View v, int position, long id)
        {
            Log.v("routes", "route selected");
        }

        public void onNothingSelected(AdapterView<?> arg0)
        {
            Log.v("routes", "nothing selected");
        }
    });
}

这是我的load_routes功能

This is my load_routes function

private void load_routes(View v)
{
    Spinner routes = (Spinner)findViewById(R.id.routes_list);
    List<String> routes_list = RouteParser.get_routes();
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, routes_list);
    routes.setAdapter(adapter);

    TableRow list_of_routes_row = (TableRow)findViewById(R.id.list_of_routes_row);
    list_of_routes_row.setVisibility(View.VISIBLE);
}

此设置不起作用。我得到这个工作的唯一方法是,当我安装我的听众为 routes.setOnItemSelectedListener(本)然后我实施 OnItemSelectedListener 和包括功能neccessary。但我有多个纺纱和需要为不同的微调单独的侦听器。任何帮助将AP preciated。谢谢!

This set up does not work. The only way I got this to work is when I setup my listener as routes.setOnItemSelectedListener(this) Then I implement OnItemSelectedListener and include the functions neccessary. But I have multiple spinners and need to create separate listeners for different spinner. Any help will be appreciated. Thanks!

推荐答案

您的声明作为微调领域实例听者一旦你做findViewById和无论你想使用它。

Declare your Spinner as field instantiate the listener once you do findViewById and use it wherever you want.

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

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