0

首先我是新手。我已经完成了如何创建微调器的示例,并且我已经在此站点和 Internet 上搜索了如何创建多个微调器,其中第二个微调器中的列表依赖于第一个微调器和第三个微调器列表依赖在第 2 次选择时。但是我在任何地方都找不到教程或解决方案。

有人可以就如何做到这一点提供一些帮助(教程会很棒:))

基本上,Spinner1 中的列表可能是“制造商”,在选择时会在 Spinner2 中生成“模型”列表,并从 Spinner2 中的模型选择中生成 Spinner3 中的“问题”列表。

任何帮助都会很棒,

先谢谢了。

4

4 回答 4

2

看这里这个演示有两个 Spinner 并且可以工作

所以onItemSelected method you can check your fisrt,second,third spinner value and set as per your requires.

于 2012-06-08T17:39:09.507 回答
1
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    Spinner spinner1,spinner2,spinner3;

    public static final String AP_DISTRICTS = "Andhra Pradesh";
    public static final String TN_DISTRICTS = "Tamil Nadu";
    public static final String TG_DISTRICTS = "Telangana";
    public static final String VC_DISTRICTS = "Victoria";
    public static final String TS_DISTRICTS = "Tasmania";
    public static final String QL_DISTRICTS = "Queens Land";
    public static final String KR_DISTRICTS = "Karachi";
    public static final String LH_DISTRICTS = "Lahore";
    public static final String SI_DISTRICTS = "Sindh";

    public static final String SELECT_COUNTRY = "--Select Country--";
    public static final String SELECT_STATE = "--Select State--";
    public static final String SELECT_DISTRICT = "--Select District--";


    String[] country = {SELECT_COUNTRY, "India", "Australia", "Pakistan"};


    String[] indiaStates = {SELECT_STATE, AP_DISTRICTS, TN_DISTRICTS, TG_DISTRICTS};
    String[] australiaStates = {"SELECT_STATE", VC_DISTRICTS, TS_DISTRICTS, QL_DISTRICTS};
    String[] pakistanStates = {"SELECT_STATE", KR_DISTRICTS, LH_DISTRICTS, SI_DISTRICTS};

    String[] apDistricts = {SELECT_DISTRICT, "Nellore", "Chittoor", "Prakasam"};
    String[] tnDistricts = {SELECT_DISTRICT, "Chennai", "Thiruvallur", "Kanchipuram"};
    String[] tgDistricts = {SELECT_DISTRICT, "Hyderabad", "Secunderabad", "Ranga Reddy"};

    String[] vicDistricts = {SELECT_DISTRICT, "Ballarat South", "Ballarat North", "Ballarat East"};
    String[] tsDistricts = {SELECT_DISTRICT, "Tasmania East", "Tasmania West", "Tasmania South"};
    String[] qsDistricts = {SELECT_DISTRICT, "Queens Land East", "Queens Land West", "Queens Land North"};


    String[] krDistricts = {SELECT_DISTRICT, "Karachi East", "Karachi North", "Karachi South"};
    String[] lhDistricts = {SELECT_DISTRICT, "Lahore South", "Lahore East", "Lahore North"};
    String[] siDistricts = {SELECT_DISTRICT, "Sindh West", "Sindh North", "Sindh East"};







    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);



        spinner1 = (Spinner) findViewById(R.id.spinner_item1);
        spinner2 = (Spinner) findViewById(R.id.spinner_item2);
        spinner3 = (Spinner) findViewById(R.id.spinner_item3);

        spinner1.setSelection(0);
        spinner2.setSelection(0);
        spinner3.setSelection(0);

        setSpinner(spinner1, country);
        setSpinner(spinner2, indiaStates);
        setSpinner(spinner3, apDistricts);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, country);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner1.setAdapter(adapter);


        spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {

                switch (i){

                    case 0:
                        Toast.makeText(MainActivity.this, "please Select Country", Toast.LENGTH_SHORT).show();
                        spinner1.setSelection(0);
                        spinner2.setSelection(0);
                        spinner3.setSelection(0);

                        break;

                    case 1:
                        setSpinner(spinner2, indiaStates);
                        break;

                    case 2:
                        setSpinner(spinner2, australiaStates);
                        break;

                    case 3:
                        setSpinner(spinner2, pakistanStates);
                        break;
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });


        spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {

                String selectedItem = adapterView.getSelectedItem().toString();

                if (spinner1.getSelectedItemPosition() == 0) {

                    Toast.makeText(MainActivity.this, "please Select Country", Toast.LENGTH_SHORT).show();

                     spinner2.setSelection(0);
                     spinner3.setSelection(0);
                    return;

                }
                if (i == 0){
                    spinner3.setSelection(0);
                    return;
                }



                switch (selectedItem){

                    case  AP_DISTRICTS:
                        setSpinner(spinner3, apDistricts);
                        break;

                    case TN_DISTRICTS :
                        setSpinner(spinner3, tnDistricts);
                        break;

                    case TG_DISTRICTS:
                        setSpinner(spinner3, tgDistricts);
                        break;

                    case VC_DISTRICTS:
                        setSpinner(spinner3, vicDistricts);
                        break;

                    case TS_DISTRICTS:
                        setSpinner(spinner3, tsDistricts);
                        break;

                    case QL_DISTRICTS:
                        setSpinner(spinner3, qsDistricts);
                        break;

                    case KR_DISTRICTS:
                        setSpinner(spinner3, krDistricts);
                        break;

                    case LH_DISTRICTS:
                        setSpinner(spinner3, lhDistricts);
                        break;

                    case SI_DISTRICTS :
                        setSpinner(spinner3, siDistricts);
                        break;

                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });


        spinner3.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                if (spinner2.getSelectedItemPosition() == 0 || spinner1.getSelectedItemPosition() == 0) {
                    Toast.makeText(MainActivity.this, "please Select State", Toast.LENGTH_SHORT).show();
                    spinner2.setSelection(0);
                    spinner3.setSelection(0);
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });









    }

    private void setSpinner(Spinner spinner2, String[] states) {

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, states);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // Specify the layout to use when the list of choices appears
        spinner2.setAdapter(adapter);
    }


}
于 2018-05-17T13:19:47.060 回答
0

这直接来自我的一个应用程序(实际上是我写的第一个应用程序),所以它不漂亮但它有效

package com.skyesmechanical.OilProductionLogApp;

import android.app.Activity;
import android.app.AlertDialog;
import android.database.Cursor;
import android.database.SQLException;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Toast;

import java.io.IOException;

public class test extends Activity {

    /** Called when the activity is first created. */
    public String spinnerPipeLengthText = "";
    public String spinnerNaturalGasText = "";
    public String spinnerPropaneGasText = "";
    public Spinner spinnerPipeLength;
    public Spinner spinnerTypeGas;
    public Spinner spinnerSupplyPressure;
    public static Integer spinnerPipeLengthInt = 0;
    public static Integer spinnerTypeGasInt = 0;
    public static Integer spinnerPropaneGasInt = 0;
    public static Integer spinnerSupplyPressureInt = 0;
    public static Integer finalRow = 0;
    public boolean supplyPressureVisible = false;
    public boolean pipeLengthVisible = false;

    static TextView textViewSize15;
    static TextView textViewSize19;
    static TextView textViewSize25;
    static TextView textViewSize31;
    static TextView textViewSize37;
    static TextView textViewSize46;
    static TextView textViewSize62;
    static TextView textViewSupplyPressure;
    static TextView textViewSelectPipeLength;

    public static Integer baseTableRowNumber = 0;   
    public static Cursor cursorResult;
    // these to int's keep a false Toast message from appearing
    private int intSpinnerCountGas = 0;
    private int intSpinnerCountSupply = 0;
    private int intSpinnerCountLength = 0;
    private static final int NO_OF_EVENTS_GAS = 1;
    private static final int NO_OF_EVENTS_SUPPLY = 1;
    private static final int NO_OF_EVENTS_LENGTH = 1;

    @Override
    public void onCreate (Bundle state) { 
        super.onCreate(state);
        setContentView(R.layout.main);
        //check if app just started or is being restored from memory
        if (state != null )  { 
            //app is being restored from memory, not executed from scratch
            //initialize the fields to the last used;
            supplyPressureVisible = state.getBoolean("supplyPressureVisible");
            pipeLengthVisible = state.getBoolean("pipeLengthVisible");
            spinnerTypeGasInt = state.getInt("spinnerTypeGasInt");
            spinnerSupplyPressureInt = state.getInt("spinnerSupplyPressureInt");
            spinnerPipeLengthInt = state.getInt("spinnerPipeLengthInt");
            finalRow = state.getInt("finalRow");

            Toast.makeText(getApplicationContext(), "savedInstanceState != null", Toast.LENGTH_LONG).show();

        }  //end if
        // call doInBackground
        new LoadDataBaseTask().doInBackground();        


        mainProgram ();
    } // end onCreate

    // performs database query outside GUI thread
    private class LoadDataBaseTask extends AsyncTask<Object, Object, Cursor> {
        DataBaseHelper myDbHelper = new DataBaseHelper(CSSTPipeSizingActivity.this);
        // perform the database access
        @Override
        protected Cursor doInBackground (Object... params) {
            try {
                myDbHelper.createDataBase();
            } 
            catch (IOException ioe) {
                throw new Error("Unable to create database");
            }
            try {
                myDbHelper.openDataBase();
            }
            catch(SQLException sqle) {
                throw sqle;
            }
            return myDbHelper.getData(); 
        } // end method doInBackground

        // use the Cursor returned from the doInBackground method
        @Override
        protected void onPostExecute(Cursor result) {
            //myDbHelper.changeCursor(result); // set the adapter's Cursor
            myDbHelper.close();
        } // end method onPostExecute
    } // end class LoadDataBaseTask 

    public void mainProgram () {
        spinnerTypeGas = (Spinner) findViewById(R.id.spinnerTypeGas);
        spinnerSupplyPressure = (Spinner) findViewById(R.id.spinnerSupplyPressure);
        spinnerPipeLength = (Spinner) findViewById(R.id.spinnerPipeLength);

        spinnerSupplyPressure.setVisibility(View.INVISIBLE);
        textViewSelectPipeLength.setVisibility(View.INVISIBLE);
        spinnerPipeLength.setVisibility(View.INVISIBLE);

        if (supplyPressureVisible == true) spinnerSupplyPressure.setVisibility(View.VISIBLE);
            else spinnerSupplyPressure.setVisibility(View.INVISIBLE);
        if (pipeLengthVisible == true) spinnerPipeLength.setVisibility(View.VISIBLE);
            else spinnerPipeLength.setVisibility(View.INVISIBLE);

        //Sets up the spinnerTypeGas spinner
        ArrayAdapter<CharSequence> adapterTypeGas = ArrayAdapter.createFromResource(
                this, R.array.TypeGas, android.R.layout.simple_spinner_item);
        adapterTypeGas.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerTypeGas.setAdapter(adapterTypeGas);

        //Sets up the spinnerPipeLength spinner 
        ArrayAdapter<CharSequence> adapterPipeLength = ArrayAdapter.createFromResource(
                this, R.array.PipeLength, android.R.layout.simple_spinner_item);
        adapterPipeLength.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerPipeLength.setAdapter(adapterPipeLength);

        // Listens for changes in the selected item in each spinner 
        spinnerTypeGas.setOnItemSelectedListener(new GASOnItemSelectedListener());
        spinnerSupplyPressure.setOnItemSelectedListener(new SupplyOnItemSelectedListener());
        spinnerPipeLength.setOnItemSelectedListener(new MyOnItemSelectedListener());
    }  // end mainProgram

    public void SpinnerNatGas() {
        ArrayAdapter<CharSequence> adapterSupplyPressure = ArrayAdapter.createFromResource(
                this, R.array.NaturalGas, android.R.layout.simple_spinner_item);
        adapterSupplyPressure.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerSupplyPressure.setAdapter(adapterSupplyPressure);
        adapterSupplyPressure.notifyDataSetChanged();
    } // end SpinnerNatGAs ()

    public void SpinnerProGas () {
        ArrayAdapter<CharSequence> adapterSupplyPressure = ArrayAdapter.createFromResource(
                this, R.array.PropaneGas, android.R.layout.simple_spinner_item);
        adapterSupplyPressure.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerSupplyPressure.setAdapter(adapterSupplyPressure);
        adapterSupplyPressure.notifyDataSetChanged();
    } // end SpinnerProGAs ()

    public class GASOnItemSelectedListener implements OnItemSelectedListener {
        public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            // this prevents false firing of Toast messages 
            if (intSpinnerCountGas < NO_OF_EVENTS_GAS) {
                intSpinnerCountGas++;
            } // end if 
            else {                
                spinnerTypeGasInt = spinnerTypeGas.getSelectedItemPosition();
                if(spinnerTypeGasInt == 1) {
                    //populate spinnerSupplyPressure accordingly
                    SpinnerNatGas();
                } // end if
                else if(spinnerTypeGasInt == 2) {
                    //populate spinnerSupplyPressure accordingly
                    SpinnerProGas();
                } // end else if
                if (spinnerTypeGasInt != 0) {
                    spinnerSupplyPressure.setVisibility(View.VISIBLE);
                    textViewSupplyPressure.setVisibility(View.VISIBLE);
                    spinnerSupplyPressure.setSelection(0);
                    spinnerPipeLength.setSelection(0);
                    supplyPressureVisible = true;
                } // end else if
                else {
                    spinnerSupplyPressure.setVisibility(View.INVISIBLE);
                    textViewSupplyPressure.setVisibility(View.INVISIBLE);
                    textViewSelectPipeLength.setVisibility(View.INVISIBLE);
                    spinnerPipeLength.setVisibility(View.INVISIBLE);
                    supplyPressureVisible = false;
                    pipeLengthVisible = false;
                }// end else
            }// end if for false Toast message at app launch
        } // end onItemSelected

        public void onNothingSelected(AdapterView<?> arg0) {
                // nothing to do
        } // end onNothingSelected
    } // end class GASOnItemSelectedListener 

    public class SupplyOnItemSelectedListener implements OnItemSelectedListener {
        public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            // this prevents false firing of Toast messages 
            if (intSpinnerCountSupply < NO_OF_EVENTS_SUPPLY) {
                intSpinnerCountSupply++;
            } // end if 
            else {                
                spinnerSupplyPressureInt = spinnerSupplyPressure.getSelectedItemPosition();
                if (spinnerSupplyPressureInt != 0) {
                    textViewSelectPipeLength.setVisibility(View.VISIBLE);
                    spinnerPipeLength.setVisibility(View.VISIBLE);
                    pipeLengthVisible = true;
                    spinnerPipeLength.setSelection(0);
                } // end if
                else {
                    textViewSelectPipeLength.setVisibility(View.INVISIBLE);
                    spinnerPipeLength.setVisibility(View.INVISIBLE);
                    pipeLengthVisible = false;
                } // end else
            }// end if for false Toast message at app launch
        } // end onItemSelected

        public void onNothingSelected(AdapterView<?> arg0) {
                // nothing to do
        } // end onNothingSelected
    } // end class SupplyOnItemSelectedListener

    public class MyOnItemSelectedListener implements OnItemSelectedListener {
        public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            if (intSpinnerCountLength < NO_OF_EVENTS_LENGTH) {
                intSpinnerCountLength++;
            } // end if 
            else {
                // This also gives the Row to look in the database for the CFH
                baseTableRowNumber = 0;
                spinnerPipeLengthInt = spinnerPipeLength.getSelectedItemPosition();
                //spinnerPipeLengthText = spinnerPipeLength.getSelectedItem().toString();
                // calculates the base table row number and stores it in variable baseTableRowNumber
                if (spinnerTypeGasInt == 1) {// Natural Gas is selected
                    baseTableRowNumber = (spinnerSupplyPressureInt - 1) * 18;
                }
                else if (spinnerTypeGasInt == 2) { // Propane is selected
                    baseTableRowNumber = 198 + (spinnerSupplyPressureInt - 1) * 18;
                } // end else if
                showResults();
            } // end else for check if firing at inializing
        } // end onItemSelected

        public void onNothingSelected(AdapterView<?> arg0) {
            // nothing to do
        } // end onNothingSelected 
    } // end class MyOnItemSelectedListener

    public void showResults () {
        if (CSSTPipeSizingActivity.cursorResult != null) { 
            finalRow = (baseTableRowNumber + spinnerPipeLengthInt);
            if (finalRow < 0) {
                finalRow = 0;
            }
            if (finalRow == 0) {
                textViewSize15.setText(String.valueOf("0"));
                textViewSize19.setText(String.valueOf("0"));
                textViewSize25.setText(String.valueOf("0"));
                textViewSize31.setText(String.valueOf("0"));
                textViewSize37.setText(String.valueOf("0"));
                textViewSize46.setText(String.valueOf("0"));
                textViewSize62.setText(String.valueOf("0"));                
            } // end if
            else {
                cursorResult.moveToPosition(finalRow);
                textViewSize15.setText(String.valueOf(cursorResult.getInt(1)));
                textViewSize19.setText(String.valueOf(cursorResult.getInt(2)));
                textViewSize25.setText(String.valueOf(cursorResult.getInt(3)));
                textViewSize31.setText(String.valueOf(cursorResult.getInt(4)));
                textViewSize37.setText(String.valueOf(cursorResult.getInt(5)));
                textViewSize46.setText(String.valueOf(cursorResult.getInt(6)));
                textViewSize62.setText(String.valueOf(cursorResult.getInt(7)));
            } // end else 
        } // end if
    } //end showResults
} // end class CSSTPipeSizingActivity

在 mainProgram() 方法的前三行中,我声明了三个微调器,然后开始填充它们。当用户从 spinnerTypeGas 中选择一个选项时,下一个微调器 (spinnerSupplyPressure) 变得可见。然后当用户从该 spinnerSupplyPressure 中选择一个项目时,第三个微调器 (spinnerPipeLenght) 变为可见。然后显示从数据库检索的数据。每个微调器从 string.xml 文件中获取其数组列表

于 2012-07-27T21:32:22.563 回答
0

您可以通过处理onItemSelected事件来做到这一点。您可以将数据存储在数据库或数组中,并且在选择时您可以在特定微调器中填充特定数组。

于 2012-06-08T17:42:00.823 回答