动态更新其他微调器的微调器

时间:2016-04-15 15:27:14

标签: android runtime spinner

我有一个在运行时创建控件的片段,首先创建一个微调器,然后是第二个,第二个微调器中的细节依赖于第一个选择。 我可以根据第一个微调器的选择来更新第二个微调器的数据吗?

<div id="value"> </div> <div id="prueba"> ABCDE </div>

      final Spinner Combo2 = new Spinner(FichaRutasVerif2SeccionSlideFragment.this.getActivity());
                        final Spinner Combo1 = new Spinner(FichaRutasVerif2SeccionSlideFragment.this.getActivity());

                        List<String> list = new ArrayList<String>();
                        list.add("TERRESTRE");
                        list.add("FLUVIAL");
                        list.add("AEREO");
                       final ArrayAdapter<String> adapterCombo = new ArrayAdapter<String>(FichaRutasVerif2SeccionSlideFragment.this.getActivity()
                                ,android.R.layout.simple_spinner_item,list);
                        List<String> list2 = new ArrayList<String>();
                        list2.add("OP1");
                        list2.add("OP2");
                        final ArrayAdapter<String> adapterCombo2 = new ArrayAdapter<String>(FichaRutasVerif2SeccionSlideFragment.this.getActivity()
                                ,android.R.layout.simple_spinner_item, list2);

                        if(listFormato.get(posicion).cSeccion.trim().equals("M"))
                        {
                            adapterCombo.setDropDownViewResource(R.layout.spinner);
                            Combo1.setAdapter(adapterCombo);
                            Combo1.setSelection(listFormato.get(posicion).iIndexCombo);
                            llTempZZ.addView(Combo1);
                            Combo1.setGravity(Gravity.CENTER);
                            llTempXX.addView(llTempZZ);
                            llTEMPADRE.addView(llTempXX);
                            pllh.addView(llTEMPADRE);

                       }

                        else if(listFormato.get(posicion).cSeccion.trim().equals("U")){
                            final Spinner Combo0 = new Spinner(FichaRutasVerif2SeccionSlideFragment.this.getActivity());
                            List<String> list3 =new ArrayList<String>();
                            list3.add("SI");
                            list3.add("NO");
                            ArrayAdapter<String> adapterCombo3 = new ArrayAdapter<String>(FichaRutasVerif2SeccionSlideFragment.this.getActivity()
                                    ,android.R.layout.simple_spinner_item, list3);
                            adapterCombo3.setDropDownViewResource(R.layout.spinner);
                            Combo0.setAdapter(adapterCombo3);
                            llTempZZ.addView(Combo0);
                            llTempXX.addView(llTempZZ);
                            //llTempXX.addView(llTempAA);
                            llTEMPADRE.addView(llTempXX);
                            pllh.addView(llTEMPADRE);
                            Combo0.setOnItemSelectedListener(new OnItemSelectedListener() {
                                public void onItemSelected(AdapterView<?> adapterView,
                                                           View view, int pos, long id) {
                                    if (pos == 0) {
                                        pll2.setVisibility(View.VISIBLE);
                                    }
                                    else if(pos==1) {
                                        pll2.setVisibility(View.GONE);
                                    }

                                    listFormato.get(posicion).vResultado = Combo0.getSelectedItem().toString();
                                    RECORDCARDITEM_DAO.Actualizar(FichaRutasVerif2SeccionSlideFragment.this.getActivity(), listFormato.get(posicion));
                                }
                                public void onNothingSelected(AdapterView<?> arg0) {
                                    // TODO Auto-generated method stub
                                }
                            });



                        }
                        else if(listFormato.get(posicion).cSeccion.trim().equals("T"))
                        {
                            adapterCombo2.setDropDownViewResource(R.layout.spinner);
                            Combo2.setAdapter(adapterCombo2);
                            //Combo1.setSelection(listFormato.get(posicion).iIndexCombo);
                            llTempZZ.addView(Combo2);
                            Combo2.setGravity(Gravity.CENTER);
                            llTempXX.addView(llTempZZ);
                            llTEMPADRE.addView(llTempXX);
                            //////TEXTOS

                            Combo2.setOnItemSelectedListener(new OnItemSelectedListener() {
                                public void onItemSelected(AdapterView<?> adapterView,
                                                           View view, int pos, long id) {
                                    if(Combo2.getSelectedItemPosition()==0)
                                    {

                                    }
                                    if (pos ==12) {
                                        llTempAA.setVisibility(View.VISIBLE);

                                    } else {
                                        llTempAA.setVisibility(View.GONE);
                                    }
                                    listFormato.get(posicion).vResultado = Combo2.getSelectedItem().toString();
                                    RECORDCARDITEM_DAO.Actualizar(FichaRutasVerif2SeccionSlideFragment.this.getActivity(), listFormato.get(posicion));
                                }
                                public void onNothingSelected(AdapterView<?> arg0) {
                                    // TODO Auto-generated method stub
                                }
                            });
                        }

1 个答案:

答案 0 :(得分:0)

你可以做这样的事情

Spinner firstSpinner = (Spinner) findViewById(R.id.first_spinner);
// You can set your first spinner values here if not set already, and then
firstSpinner.setOnItemSelectedListener(onFirstSpinnerChange(firstSpinner));

进一步添加此

AdapterView.OnItemSelectedListener onFirstSpinnerChange(Spinner mySpinner) {
    return new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
            // You can add this method to set the values of the second spinner in your activity
            // You can use the position, or alternatively selectedItemView for this purpose to identify the selected item
            setSecondSpinnerBasedOnFirstOnePositionSelected();

            // Or you can just set your spinner in this block
        }

        @Override
        public void onNothingSelected(AdapterView<?> parentView) {
            // do nothing
        }
    };
}