menu FixWins.com Spring MVC Spring Boot NodeJS+ExpressJS+Mysql ReactJS+NodeJS+Mysql ReactJS+Redux :CRUD Android PFE
Spring MVC Spring Boot NodeJS+ExpressJS+Mysql ReactJS+NodeJS+Mysql ReactJS+Redux :CRUD PFE

Android : SelectedListener Classe et menu

Une fois, on sélectionne une classe de menu, on remplit le spinner menu , au choix d'un menu, on affiche la liste des plats.

1.Layout

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="3dp"
android:background="#FFEB3B">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#CDDC39"
android:layout_weight="3.6">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#FCFDFC"
android:layout_marginBottom="5dp">
<TextView
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_weight="1"
android:text="Classe"
android:textSize="25dp"/>
<Spinner
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_weight="1"
android:id="@+id/spinnerClasse"
android:textSize="200dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#FCFDFC"
android:layout_marginBottom="5dp">
<TextView
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_weight="1"
android:text="Menu"
android:textSize="25dp"/>
<Spinner
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_weight="1"
android:id="@+id/spinnerMenu"
android:textSize="200dp"/>
</LinearLayout>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="4"
android:text="Liste Plats"
android:textSize="25dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#8BC34A"
android:layout_weight="1"
android:paddingTop="8dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fff"
android:textSize="20dp"
android:text="Nom "/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
android:background="#fff"
android:layout_marginTop="2dp"
android:id="@+id/listePlats"/>
</LinearLayout>
</LinearLayout>

2.Java


public class MenuClasse extends AppCompatActivity implements AdapterView.OnItemSelectedListener{
Spinner spinnerClasse,spinnerMenu;
TextView listePlats;
String []classes={"Classe1","Classe2","Classe3"};
String [][]menus={
{"Menu 1 Classe1","Menu 2 Classe1","Menu 3 Classe1"},
{"Menu 1 Classe2","Menu 2 Classe2","Menu 3 Classe2"},
{"Menu 1 Classe3","Menu 2 Classe3","Menu 3 Classe3"}
};
int classeChosi=0;
int menuChoisi=0;
String []plats={
"C1 M1 plat1 \n C1 M1 plat2 ",
"C1 M2 plat1 \n C1 M2 plat2 ",
"C1 M3 plat1 \n C1 M3 plat2 ",
"C2 M1 plat1 \n C2 M1 plat2 ",
"C2 M2 plat1 \n C2 M2 plat2 ",
"C2 M3 plat1 \n C2 M3 plat2 ",
"C3 M1 plat1 \n C3 M1 plat2 ",
"C3 M2 plat1 \n C3 M2 plat2 ",
"C3 M3 plat1 \n C3 M3 plat2 ",
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu_classe);
//1.la liaison entre les objet java et les objets graphique
spinnerClasse=findViewById(R.id.spinnerClasse);
spinnerMenu=findViewById(R.id.spinnerMenu);
listePlats=findViewById(R.id.listePlats);
//2.ecouter les objets cliquables
spinnerClasse.setOnItemSelectedListener(this);
spinnerMenu.setOnItemSelectedListener(this);
//3.Remplir le spinner
ArrayAdapter<String>adpater1=new ArrayAdapter<>(this,R.layout.support_simple_spinner_dropdown_item,classes);
spinnerClasse.setAdapter(adpater1);
}
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
if(adapterView.getId()==spinnerClasse.getId())
{
int positionClass=spinnerClasse.getSelectedItemPosition();
classeChosi=positionClass;
String[] selectedMenu=new String[3];
selectedMenu[0]=menus[positionClass][0];
selectedMenu[1]=menus[positionClass][1];
selectedMenu[2]=menus[positionClass][2];
ArrayAdapter<String>adapter2=new ArrayAdapter<>(this,R.layout.support_simple_spinner_dropdown_item,selectedMenu);
spinnerMenu.setAdapter(adapter2);
}
if(adapterView.getId()==spinnerMenu.getId()) {
int positionMenu = spinnerMenu.getSelectedItemPosition();
String menuChosi=spinnerMenu.getSelectedItem().toString();
menuChoisi=positionMenu;
String monPlats=plats[3*classeChosi+menuChoisi];
listePlats.setText(monPlats);
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
}