如何根据spinner1选择更新spinner2 [英] how to update spinner2 based on spinner1 choice

查看:151
本文介绍了如何根据spinner1选择更新spinner2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要开发基于微调的应用。

I have to develop the application based on spinner.

所以我采取了spinner1作为产品选择,我得给spinner2的数据。这是在那个特定的产品类别,所以我必须在基地选择spinner1的更新spinner2。

so i have taken the spinner1 as the product choice and i have to give the spinner2 the data. which is in that particular product category so i have to update spinner2 on the base choice of spinner1.

我搜索了很多,但我可以肯定任何例如code或种源吧。所以请帮助我这样做。

i have search a lot but i can get any sure example code or resouce for it . so please help me to do so.

推荐答案

我同意@ user639183,也有类似的问题很多的......不过,你是怎么做到的一些说明:

I agree with @user639183, there are lot's of similar questions... however, there some explanation on how you do it:


  1. 创建为数组成员变量包含你的选项来显示。

  2. 您的类别值填充你的第一个微调。

  3. 挂钩上Spinner的 OnItemSelected 事件通过使用 spinner1.setOnItemSelectedListener(...)

  4. 在事件监听器,填充你的第二个微调相应的值。

  1. Create member variables for the arrays containing your options to display.
  2. Populate your first spinner with your category values.
  3. Hook on to the Spinner's OnItemSelected event by using spinner1.setOnItemSelectedListener(...)
  4. In the event listener, populate your second Spinner with the corresponding values.

举例步骤1:

private String[] spinner1values = new String[] { "cat1", "cat2" };
private String[][] spinner2values = new String[][] {
        new String[] { "a1", "b1", "c1" },
        new String[] { "a2", "b2" }
};

spinner1人口如下:

Population of spinner1 as follows:

Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);
    ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, spinner1values);
    adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner1.setAdapter(adapter1);

这至今是绝对简单的,如果你读了纱厂文档和例子!

This has so far been absolutely straight forward, if you read the documentation and examples for Spinners!

接下来,钩到 OnItemSelectedListener

spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1,
                int position, long arg3) {
            // create a new adapter with the corresponding values
            ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(arg0.getContext(),
                    android.R.layout.simple_spinner_item, spinner2values[position]);
            adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            // set adapter
            ((Spinner)findViewById(R.id.spinner2)).setAdapter(adapter2);
        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // nothing selected, so set empty options
            ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(arg0.getContext(),
                    android.R.layout.simple_spinner_item, new String[0]);
            adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            ((Spinner)findViewById(R.id.spinner2)).setAdapter(adapter2);
        }
    });

注意的是,该阵列中的 spinner2values​​ 的顺序对应的类别值的顺序!

Pay attention, that the order of the arrays in spinner2values is corresponding to the order of the category values!

这篇关于如何根据spinner1选择更新spinner2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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