Android : OnItemSelectedListener

Ecoutez un le changement d'une valeur d'un spinner 

1.Layout

 <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:background="#8BC34A">
  <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:orientation="horizontal"
      android:background="#454f">
           <TextView
               android:layout_width="match_parent"
               android:layout_height="wrap_content"
               android:layout_weight="2"
               android:text="Pays:"
               android:textSize="23dp"/>


        <Spinner
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:id="@+id/listeCountries"/>
  </LinearLayout>
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="20dp"
    android:text="liste villes"
    android:id="@+id/txtcities"/>

    </LinearLayout>

2.Java


public class CountriesCities extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
    Spinner SpinnerlisteCountries;
    TextView txtcities;
    ArrayList<String[]> paysCity=new ArrayList<String[]>();
    String[] a={"casa","Rabat","dakhla","tanger"};
    String[] b={"paris","nice","abc","nante"};
    String[] c={"madrid","bacelone","malaga","almeria"};
   
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_countries_cities);
   
            SpinnerlisteCountries=findViewById(R.id.listeCountries);
            txtcities=findViewById(R.id.txtcities);
   
            SpinnerlisteCountries.setOnItemSelectedListener(this);
            //remplir le spinner des countries
            String[]pays={"Maroc","France","Espagne"};
            ArrayAdapter ad=new ArrayAdapter<String>(this,R.layout.support_simple_spinner_dropdown_item,pays);
            SpinnerlisteCountries.setAdapter(ad);
   
            //remplir les pays et les villes
   
            paysCity.add(a);
            paysCity.add(b);
            paysCity.add(c);
   
   
        }
   
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
   
           if(adapterView.getId()==SpinnerlisteCountries.getId()) {
               txtcities.setText("");
               int positionChoisie =SpinnerlisteCountries.getSelectedItemPosition();
               String[] villesAfficher=paysCity.get(positionChoisie);
               for(int j=0;j<villesAfficher.length;j++)
               {
                   txtcities.append("\n"+villesAfficher[j]);
               }
   
           }
        }
   
        //si on a rien sélectionné
        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {
            txtcities.setText("Veuillez sélectionner un pays");
        }
    }