0

I am having a spinner that should be filled with strings.But can not use binding.mySpinner to set adapter. The Goal is to have two spinners first is with two country USA and CANADA so when user click on Usa second spinner should list all states.Same thing for Canada.I managed to find out what country is clicked and i stored that value in String.I wanted to use simple If/else statement but do not know how to fill second spinner.

my xml:`

<Spinner
                android:id="@+id/states_spinner"
                style="@style/Widget.AppCompat.DropDownItem.Spinner"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginBottom="50dp"
                app:layout_constraintBottom_toTopOf="@+id/submit_BTN"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/states_TV" />

`

and my ViewModel:

     public AdapterView.OnItemSelectedListener countryListener = new AdapterView.OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            Object item = parent.getItemAtPosition(position).toString();

            switch (position){
                case 0:
                    billingInfo.setCountry("Usa");
                    selectedCountry = billingInfo.getCountry();
                    List<String> listUsa = new ArrayList<String>(Arrays.asList(SpinnerEntriesUtils.usaArray));
                    ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                            SuccessPlanApplication.getContext(), android.R.layout.simple_spinner_item,listUsa );

                    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

                  
                    break;
                case 1:
                    billingInfo.setCountry("Canada");
                    selectedCountry = billingInfo.getCountry();

                 
                    break;
            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    };
gojic
  • 363
  • 7
  • 20
  • Look at this answer on how populate a spinner programmatically: https://stackoverflow.com/a/11920785/9796205 – Francesco Re Aug 27 '20 at 16:51
  • @FrancescoRe i can't do this because i don't know how to set adapter in in ViewModel..i can not use findById – gojic Sep 01 '20 at 10:01
  • Why are you using a ViewModel in that way? it should be used just to store UI data but the UI controller should be in the activity (or fragment). However, if you really need to use the ViewModel like that, you can pass the context (and do context.findViewById()) or pass directly the spinner reference to the view model. – Francesco Re Sep 01 '20 at 16:54

0 Answers0