简体   繁体   中英

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.

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;
                }
            }

        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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