【问题标题】:Dynamic Spinners in Android (General Workflow Question)Android 中的动态微调器(一般工作流程问题)
【发布时间】:2012-07-27 18:45:01
【问题描述】:

就像这个one,我在这个网站上看到了一些操作方法,但说实话,我并没有真正了解它

我希望一个微调器的内容基于之前的微调器选择,例如在州和城市场景中。一般而言,工作流程是什么?第二个微调器的结果是根据第一个微调器过滤的,还是第二个微调器指向一个基于第一个微调器的完全不同的列表?

对于我自己的简单学习项目,我在 strings.xml 中构建了几个字符串数组(AL-Cities、AK-Cities、AR-Cities 等)。我希望城市微调器根据州微调器的选择从正确的数组中填充。但我想知道是否应该只使用一个大型多维“城市”数组,其中包含州缩写作为附加标识符,然后使用州缩写作为过滤器将第二个微调器指向该数组。看起来前者会提供更好的性能。

任何帮助(和代码示例)将不胜感激。我对编程并不陌生(主要是 php,所以我猜脚本更准确),但我是 java 新手。到目前为止,我没有链接微调器的代码如下,第二个微调器指向未区分的 city_array。

谢谢!

public class Example1 extends Activity {

/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.example1);

   Spinner spinState = (Spinner) findViewById(R.id.spin_state);
    ArrayAdapter<CharSequence> adapter3 = ArrayAdapter.createFromResource(
            this, R.array.state_array, android.R.layout.simple_spinner_item);
    adapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinState.setAdapter(adapter3);

    Spinner spinCity = (Spinner) findViewById(R.id.spin_city);
    ArrayAdapter<CharSequence> adapter4 = ArrayAdapter.createFromResource(
            this, R.array.city_array, android.R.layout.simple_spinner_item);
    adapter4.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinCity.setAdapter(adapter4);
}
}

【问题讨论】:

    标签: java android spinner


    【解决方案1】:

    您可以尝试从您选择的第一个微调器中获取位置,然后在根据该位置检索正确的数组后填充您的第二个微调器。

    你必须监听你的第一个适配器被改变:

        spinner. setOnItemSelectedListener(new MyOnItemSelectedListener());
    
    
    class MyOnItemSelectedListener implements OnItemSelectedListener {
        public void onItemSelected(AdapterView<?> parent,
            View view, int pos, long id) {
            String choice = parent.getItemAtPosition(pos).toString();
                populateSecondSpinnerMethod(choice)
           }
        }
        public void onNothingSelected(AdapterView parent) {       // Do nothing.
        }
    }
    

    【讨论】:

    • 所以真正的魔法发生在“populateSecondSpinnerMethod()”中?你会用 if/else 或 switch/case 进行评估,然后说“如果选择 = 佛罗里达州,spinner2.setAdapter...FL-Cities”吗?
    • 抱歉 Pr0t0,超级错过了这个>_
    猜你喜欢
    • 2016-06-08
    • 2017-02-14
    • 1970-01-01
    • 1970-01-01
    • 2020-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多