微调器中的项目选择在MyOnItemSelectedListener中无法检测

时间:2015-04-10 17:03:18

标签: android android-spinner

我有3个微调器的活动,第1个微调器的选择与第2个和第2个选择有关,与第3个相关,所以我有一个旋转器代码。 CrustSp, SizeSp and extraDescriptionOneSP是我拥有的3个微调器。我已将项目加载到Spinners,我也可以选择一个值。但我的问题是每当我在CrustSp中做出选择时,SizeSp都没有在MyOnItemSelectedListener().中显示但是如果我在extraDescriptionOneSP中进行了选择,它会显示在MyOnItemSelectedListener()内。

我无法弄清楚我哪里出错,任何人都可以指出我出错的地方会非常有帮助。

这个代码

            crust = Utils.removeDuplicatesFromList(crust);
            ArrayAdapter<String> dataAdapterCru = new ArrayAdapter<String>(
                    this, android.R.layout.simple_spinner_item, crust);
            dataAdapterCru
                    .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            crustSP.setAdapter(dataAdapterCru);

            crustSP.setOnItemSelectedListener(new MyOnItemSelectedListener(
                    ActivityPizzaCustomize.this) {

                public void onNothingSelected(AdapterView<?> arg0) {
                }

                @Override
                public void onItemSelected(AdapterView<?> parent,
                        View view, int position, long id) {

                    String crustSelectedItem = crustSP.getSelectedItem()
                            .toString();

                    List<String> resultSizes = getFilteredSizes(crustSelectedItem);

                    ArrayAdapter<String> dataAdapterDes = new ArrayAdapter<String>(
                            ActivityPizzaCustomize.this,
                            android.R.layout.simple_spinner_item,
                            resultSizes);
                    dataAdapterDes
                            .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                    sizeSP.setAdapter(dataAdapterDes);

                    sizeSP.setOnItemSelectedListener(new MyOnItemSelectedListener(
                            ActivityPizzaCustomize.this) {

                        public void onNothingSelected(AdapterView<?> arg0) {
                        }

                        @Override
                        public void onItemSelected(AdapterView<?> parent,
                                View view, int position, long id) {

                            String sizeSelectedItem = sizeSP
                                    .getSelectedItem().toString();

                            List<String> resultTopping = getFilteredToppings(sizeSelectedItem);

                            extraDescriptionOneSP
                                    .setOnItemSelectedListener(new MyOnItemSelectedListener(
                                            ActivityPizzaCustomize.this));
                            ArrayAdapter<String> dataAdapterExtraDesOne = new ArrayAdapter<String>(
                                    ActivityPizzaCustomize.this,
                                    android.R.layout.simple_spinner_item,
                                    resultTopping);
                            dataAdapterExtraDesOne
                                    .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                            extraDescriptionOneSP
                                    .setAdapter(dataAdapterExtraDesOne);
                            extraDescriptionOneSP
                                    .setAdapter(new NothingSelectedSpinnerAdapter(
                                            dataAdapterExtraDesOne,
                                            R.layout.contact_spinner_row_nothing_selected,
                                            ActivityPizzaCustomize.this));

                        }

                    });

                }
            });

MyOnItemSelectedListener方法,

public class MyOnItemSelectedListener实现OnItemSelectedListener {

    Activity activity;
    String StrOneSPPosition;

    public MyOnItemSelectedListener(Activity p_activity) {
        activity = p_activity;
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view,
            int position, long id) {

        // check which spinner triggered the listener
        switch (parent.getId()) {

        case R.id.sp_crust: {

            String StrCrustSPPosition = String.valueOf(crustSP
                    .getSelectedItemPosition());
            System.out.println("crust = " + StrCrustSPPosition);
            System.out.println("crust = " + position);
            CrustSPPosition = Integer.parseInt(StrCrustSPPosition);

        }

1 个答案:

答案 0 :(得分:0)

我没有看到您使用所选位置来确定下一个微调器的数据。顺便说一句,你不应该为每个选择制作新的适配器,而是重复使用曾经制造的适配器。