简体   繁体   English

从下拉列表中选择,第二个下拉列表中的选项

[英]Select from a drop down list, options in a second drop down list

I'm new to this forum, and really hope somebody could help me, as I've been searching Google endlessly the last few days. 我是这个论坛的新手,我真的希望有人能为我提供帮助,因为最近几天我一直在不断搜索Google。

I'm writing an app where you can choose a location, ex. 我正在编写一个可以在其中选择位置的应用程序,例如。 grocery store, petrol station, and also a scenario like asking if you can pay by card. 杂货店,加油站,以及诸如询问是否可以通过卡付款的场景。
The scenario then gets translated to another language. 然后将方案翻译成另一种语言。

What I'm trying to accomplish is the following: 我要完成的工作如下:

I would like to select a location from a drop down list, which in turn would give me options for that selection in a second drop down list. 我想从一个下拉列表中选择一个位置,这又会在第二个下拉列表中为我提供选择的位置。

Example: 例:

SELECT grocery store FROM first drop down

The second drop down must populate with scenarios that you would use in a grocery store 第二个下拉列表必须填充您将在杂货店中使用的方案

SELECT petrol station FROM first drop down

The second drop down must populate with scenarios that you would use at petrol station. 第二个下拉列表必须填充您将在加油站使用的方案。

I will post my code, if needed! 如果需要,我将发布我的代码!

在第一个组合上附加一个选择侦听器,在其中您将填充第二个组合的选择选项。

I've finally got it right. 我终于正确了。

listPlaces = (Spinner)findViewById(R.id.spnPlaces);
    listScenarios = (Spinner)findViewById(R.id.spnScenario);
    String[] places = new String[] {"At the Supermarket","At the Petrol Station", "At The Spur"};       

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, places);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    listPlaces.setAdapter(adapter);

    listPlaces.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View view,
                int position, long id) {
            // TODO Auto-generated method stub

            switch (position){
            case 0:{
                List <String> list = new ArrayList<String>();
                list.add("Tea");
                list.add("Coffee");
                list.add("Sugar");
                list.add("Milk");
                ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(Options.this, android.R.layout.simple_spinner_dropdown_item, list);
                dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                dataAdapter.notifyDataSetChanged();
                listScenarios.setAdapter(dataAdapter);
                break;
            }
            case 1:{
                List <String> list = new ArrayList<String>();
                list.add("Bread");
                list.add("Butter");
                list.add("Eggs");
                list.add("Bacon");
                ArrayAdapter<String> dataAdapter2 = new ArrayAdapter<String>(Options.this, android.R.layout.simple_spinner_dropdown_item, list);
                dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                dataAdapter2.notifyDataSetChanged();
                listScenarios.setAdapter(dataAdapter2);
                break;
                }
            }

        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM