Android Spinner不会在MyOnItemSelectedListener中显示所选项目位置

时间:2015-03-18 07:09:40

标签: android android-spinner

这些是我在我的应用程序中使用的3个微调器。 crustSP,sizeSP和extraDescriptionOneSP。功能vise这三个微调工作完美(加载项目,可以根据需要选择任何项目),但我的问题是在crustSP和sizeSP微调器,它没有显示MyOnItemSelectedListener中的选定项目位置。它显示extraDescriptionOneSP的选定项目位置。

任何人都能想出crustSP和sizeSP微调器的问题。我很想要所选的项目位置。

                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 OnItemSelectedListener() {

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

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

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

                        List<String> resultDescription = getFilteredDescriptions(
                                crustSelectedItem, description);
                        ArrayAdapter<String> dataAdapterDes = new ArrayAdapter<String>(
                                ActivityPizzaCustomize.this,
                                android.R.layout.simple_spinner_item,
                                resultDescription);
                        dataAdapterDes
                                .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                        sizeSP.setAdapter(dataAdapterDes);
                        sizeSP.setAdapter(new NothingSelectedSpinnerAdapter(
                                dataAdapterDes,
                                R.layout.contact_spinner_row_nothing_selected,
                                ActivityPizzaCustomize.this));
                    }
                });

                sizeSP.setOnItemSelectedListener(new OnItemSelectedListener() {

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

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

                    }
                });

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

myOnItemSelectedListener代码,

public class MyOnItemSelectedListener implements OnItemSelectedListener {

        @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:


                System.out.println("crust = " + position);
                break;

            case R.id.sp_pizza_size:


                System.out.println("size = " + position);
                break;

            case R.id.sp_extratoppingone:


                System.out.println("extratoppingone = " + position);
                break;

2 个答案:

答案 0 :(得分:1)

试试这个,这适用于你

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> resultDescription = getFilteredDescriptions(
                                    crustSelectedItem, description);
                            sizeSP
                            .setOnItemSelectedListener(new MyOnItemSelectedListener(ActivityPizzaCustomize.this));
                            ArrayAdapter<String> dataAdapterDes = new ArrayAdapter<String>(
                                    ActivityPizzaCustomize.this,
                                    android.R.layout.simple_spinner_item,
                                    resultDescription);
                            dataAdapterDes
                                    .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                            sizeSP.setAdapter(dataAdapterDes);
                            sizeSP.setAdapter(new NothingSelectedSpinnerAdapter(
                                    dataAdapterDes,
                                    R.layout.contact_spinner_row_nothing_selected,
                                    ActivityPizzaCustomize.this));
                        }
                    });

答案 1 :(得分:0)

问题在于,您必须将相同的MyOnItemSelectedListener设置为custSP和sizeSP。

extraDescriptionOneSP.setOnItemSelectedListener(new MyOnItemSelectedListener(ActivityPizzaCustomize.this));
custSP.setOnItemSelectedListener(new MyOnItemSelectedListener(ActivityPizzaCustomize.this));
sizeSP.setOnItemSelectedListener(new MyOnItemSelectedListener(ActivityPizzaCustomize.this));

为了保持逻辑,您应该对MyOnItemSelectedListener进行一些更改,并在所选的每个微调器项上添加您想要执行的操作,如下所示:

public class MyOnItemSelectedListener implements OnItemSelectedListener 
{
   Activity activity;

    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:
         {
                System.out.println("crust = " + position);

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

                        List<String> resultDescription = getFilteredDescriptions(
                                crustSelectedItem, description);
                        ArrayAdapter<String> dataAdapterDes = new ArrayAdapter<String>(
                                activity,
                                android.R.layout.simple_spinner_item,
                                resultDescription);
                        dataAdapterDes
                                .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                        sizeSP.setAdapter(dataAdapterDes);
                        sizeSP.setAdapter(new NothingSelectedSpinnerAdapter(
                                dataAdapterDes,
                                R.layout.contact_spinner_row_nothing_selected,
                                activity));

            }
            break;

        case R.id.sp_pizza_size:


            System.out.println("size = " + position);
            break;

        case R.id.sp_extratoppingone:


            System.out.println("extratoppingone = " + position);
            break;