SetSelection在android spinner中不起作用

时间:2018-03-20 14:04:03

标签: java android android-fragments android-spinner

我正在开发一个Android过滤器,我使用多个微调器,问题是,当我习惯setSelection选项时,它没有显示我正确的位置。但是当我在按钮中使用相同的功能时,它会显示正确的选择。 我想知道如何在创建视图中设置选择位置。 这是我的代码:

   @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        Log.d("MAP_FRAG","onCreateView");
        final MySharedPreferences sp = new MySharedPreferences(getActivity());
        // Inflate the layout for this fragment
        final View rootView = inflater.inflate(R.layout.fragment_fragment__search, container, false);

        getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

        ((AppCompatActivity)getActivity()).getSupportActionBar().setTitle("Search Filter");

        countryAdapter = new ArrayAdapter<Country>(getActivity(), android.R.layout.simple_spinner_item, countryArrayList);


//        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getActivity(),
//                android.R.layout.simple_dropdown_item_1line, SPINNERLIST);


        FloatingActionButton fab = (FloatingActionButton) rootView.findViewById(R.id.fabSelec);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


                fragment_search_customerProfile customerProfile = new fragment_search_customerProfile();
                FragmentManager manager = getActivity().getSupportFragmentManager();
                FragmentTransaction transaction = manager.beginTransaction();
//                transaction.replace(R.id.filterFram, customerProfile);
                transaction.add(R.id.filterFram, customerProfile);
                transaction.addToBackStack(null);

//                FrameLayout layout = (FrameLayout) rootView.findViewById(R.id.filterFram);
//                layout.removeAllViewsInLayout();

                Bundle bundle = new Bundle();
                bundle.putParcelable("selectedFilterObj", selectedFilter);
                Log.d("TEST ...","getting filter set "+selectedFilter.getSelectedZondPosition());
                Log.d("TEST ...","getting filter set "+selectedFilter.getSelectedZondName());
                customerProfile.setArguments(bundle);

                sp.saveSelectedFilterObj(selectedFilter);


                transaction.commit();


//                synchronizeArrays();

            }
        });



        mdsForCountry = (Spinner) rootView.findViewById(R.id.spCountry);
        mdsForZone = (Spinner) rootView.findViewById(R.id.spZone);

        mdsForRegion = (Spinner) rootView.findViewById(R.id.spRegion);
        mdsForCity = (Spinner) rootView.findViewById(R.id.spCity);
        mdsForArea = (Spinner) rootView.findViewById(R.id.spArea);
        mdsForMarket = (Spinner) rootView.findViewById(R.id.spMarket);

        mdsForCountry.setAdapter(countryAdapter);
        mdsForZone.setAdapter(zoneAdapter);
        mdsForRegion.setAdapter(regionAdapter);
        mdsForCity.setAdapter(cityAdapter);
        mdsForArea.setAdapter(areaAdapter);
        mdsForMarket.setAdapter(marketAdapter);




        mdsForCountry.setBackgroundResource(R.drawable.spinner_background);
        mdsForZone.setBackgroundResource(R.drawable.spinner_background);
        mdsForRegion.setBackgroundResource(R.drawable.spinner_background);
        mdsForCity.setBackgroundResource(R.drawable.spinner_background);



        mdsForCountry.setOnItemSelectedListener(this);
        mdsForZone.setOnItemSelectedListener(this);
        mdsForRegion.setOnItemSelectedListener(this);
        mdsForCity.setOnItemSelectedListener(this);
        mdsForArea.setOnItemSelectedListener(this);
        mdsForMarket.setOnItemSelectedListener(this);


        selectedFilter = sp.getSelectedFilterObj();
        if(selectedFilter != null){
            Log.d("TEST ...","getting filter");

            Log.d("TEST ...","getting getSelectedCountryPosition : "+selectedFilter.getSelectedCountryPosition());
            Log.d("TEST ...","getting getSelectedZondPosition : "+selectedFilter.getSelectedZondPosition());
            Log.d("TEST ...","getting getSelectedRegionPosition : "+selectedFilter.getSelectedRegionPosition());
            Log.d("TEST ...","getting getSelectedCityPosition : "+selectedFilter.getSelectedCityPosition());



            mdsForCountry.setSelection(selectedFilter.getSelectedCountryPosition(), false);
            mdsForZone.setSelection(selectedFilter.getSelectedZondPosition(),false);
            mdsForRegion.setSelection(selectedFilter.getSelectedRegionPosition(),false);
            mdsForCity.setSelection(selectedFilter.getSelectedCityPosition());
            mdsForArea.setSelection(selectedFilter.getSelectedAreaPosition());
            mdsForMarket.setSelection(selectedFilter.getSelectedMarketPosition());

        }



        return rootView;
    }

0 个答案:

没有答案