如何设置一个微调器的选定项目控制其他微调器的项目?

时间:2014-02-17 03:52:11

标签: java android android-spinner

您好我正在构建转换器。我有3个微调器。一个是单位类型...其他是From:和To:我想设置如果从“单位类型”微调器“时间”被选中,那么其他两个微调器变为“分钟,秒,小时等等”。然后,如果我选择质量,其他两个微调器改为“Gram Kg milligram etc etc”。我将单位存储在数组中。我有3个适配器用于3个微调器。现在我如何将“单位类型”微调器连接到这两个? '

 private static String[] types = { "Angle", "Area", "Bits&Bytes", "Density",
        "Electric Current", "Energy", "Force", "Length", "Mass", "Power",
        "Pressure", "Speed", "Temperature", "Time", "Volume" };

private static String[] AngleUnits = { "Degree", "Radian", "Grad", "Gon",
        "Minute", "Second", "SIgn", "Mil", "Revolution", "Circle", "Turn",
        "Quadrant", "Right", "Triangle", "Sextant" };

private static String[] AreaUnits = { "SquareMeter", "SquareKilometer",
        "SquareHectometer", "SquareDekameter", "SquareDecimeter",
        "SquareCentimeter", "SquareMilimeter", "SquareMicrometer",
        "SquareNanometer", "Hectare", "SquareMile", "SquareYard",
        "SquareFoot", "SquareInch", "Acre" };

private static String[] BitsBytesUnits = { "bit", "byte", "kilobit",
        "kilobyte", "megabit", "megabyte", "gigabit", "gigabyte",
        "terabit", "terabyte", "petabit", "petabyte", "exabit", "exabyte" };

private static String[] DensityUnits = { "kilogram/cubicmeter",
        "kilogram/cubiccentimeter", "gram/cubicmeter",
        "gram/cubiccentimeter", "gram/cubicmilimeter",
        "miligram/cubicmeter", "miligram/cubiccentimeter",
        "miligram/cubicmilimeter", "exagram/liter", "petagram/liter",
        "teragram/liter", "gigagram/liter", "meggram/liter",
        "kilogram/liter", "hectogram/liter", "decagram/liter",
        "gram/liter", "decigram/liter", "centigram/liter",
        "miligram/liter", "microgram/liter", "nanogram/liter",
        "picogram/liter" };

private static String[] CurrentUnits = { "ampere", "kiloampere",
        "miliampere", "biot", "abaampere", "stataampere" };

private static String[] EnergyUnits = { "joule", "gigajoule", "megajoule",
        "kilojoule", "milijoule", "microjoule", "nanojoule", "attojoule" };

private static String[] ForceUnits = { "newton", "dyne", "joul/meter",
        "joul/centimeter", "gram-force", "kilogram-force", "kip-force",
        "pond", "kilo-pond" };

private static String[] LengthUnits = { "meter", "exameter", "terameter",
        "petameter", "gigameter", "megameter", "kilometer", "hectometer",
        "dekameter", "decimeter", "centimeter", "milimeter", "micrometer",
        "nanometer", "picometer", "femtometer", "attometer" };

private static String[] MassUnits = { "gram", "kilogram", "miligram",
        "dalton", "pound", "ounce", "ton", "quarter", "grain" };

private static String[] PowerUnits = { "watt", "megawatt", "horsepower",
        "kilocalorie", "joul/hour", "kilojoul/hour", "joul/second" };

private static String[] PressureUnits = { "pascal", "newton/squaremeter",
        "bar", "kilogram-force/squaremeter", "ton-force/squarefoot",
        "poundal/squarefoot", "atmosphere(atm)" };

private static String[] SpeedUnits = { "meter/second", "kilometer/hour",
        "foot/hour", "yard/hour", "mile/hour", "mile/second", "knot" };

private static String[] TemperatureUnits = { "kelvin", "celsius",
        "farenheit", "rankine", "reaumur" };

private static String[] TimeUnits = { "second", "milisecond",
        "microsecond", "nanosecond", "minute", "hour", "day", "week",
        "month", "year", "decade", "century", "millenium" };

private static String[] VolumeUnits = { "cubicmeter", "cubickilometer",
        "cubiccentimeter", "liter", "cc", "barrel", "cup", "cubicmile",
        "cubicfoot", "cubicyard", "cubicinch", "acre-foot" };

         private Spinner spnUnitTypes;
         private Spinner spnFromUnit;
         private Spinner spnToUnit;

        ArrayAdapter<String> adaptUnittype;
        ArrayAdapter<String> adaptFrom;
        ArrayAdapter<String> adaptTo;

         @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);



             // initialize
    spnUnitTypes = (Spinner) findViewById(R.id.spnUnitType);
    spnFromUnit = (Spinner) findViewById(R.id.spnFromUnit);
    spnToUnit = (Spinner) findViewById(R.id.spnToUnit);

    // initialize adapter
    adaptUnittype = new ArrayAdapter<String>(MainActivity.this,
            android.R.layout.simple_spinner_item, types);

              //bind
             spnUnitTypes.setAdapter(adaptUnittype);

但是如何连接这个spnUnitType的选定项目控制其他微调器?

1 个答案:

答案 0 :(得分:1)

看看这段代码,然后用任何不明显的东西喂我回来

    spnUnitTypes.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1,
                int postion, long arg3) {

            if (types[postion].equals("Time")){

                adaptFrom = new ArrayAdapter<String>(SpinnerActivity.this, android.R.layout.simple_spinner_item, TimeUnits);
                adaptTo = new ArrayAdapter<String>(SpinnerActivity.this, android.R.layout.simple_spinner_item, TimeUnits);

        }else if (types[postion].equals("Mass")){

            adaptFrom = new ArrayAdapter<String>(SpinnerActivity.this, android.R.layout.simple_spinner_item, MassUnits);
            adaptTo = new ArrayAdapter<String>(SpinnerActivity.this, android.R.layout.simple_spinner_item, MassUnits);
        }

            spnFromUnit.setAdapter(adaptFrom);
            spnToUnit.setAdapter(adaptTo);



        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub

        }
    });