Skip to content
Snippets Groups Projects
Commit 545129ff authored by Sofiane Lasri's avatar Sofiane Lasri
Browse files

Partie 3: Persistance courte

parent db671e89
No related branches found
No related tags found
No related merge requests found
Pipeline #3 canceled
package com.slprojects.pizzeria; package com.slprojects.pizzeria;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle; import android.os.Bundle;
...@@ -21,53 +22,63 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe ...@@ -21,53 +22,63 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); setContentView(R.layout.activity_main);
boutons = new ArrayList<Button>(); boutons = new ArrayList<>();
idIndexes = new ArrayList<Integer>(); idIndexes = new ArrayList<>();
numCommands = new ArrayList<Integer>(); numCommands = new ArrayList<>();
nomBoutons = new ArrayList<String>(); nomBoutons = new ArrayList<>();
boutons.add(findViewById(R.id.napolitaine)); boutons.add(findViewById(R.id.napolitaine));
idIndexes.add(R.id.napolitaine);
numCommands.add(idIndexes.indexOf(R.id.napolitaine), 0);
boutons.add(findViewById(R.id.royale)); boutons.add(findViewById(R.id.royale));
idIndexes.add(R.id.royale);
numCommands.add(idIndexes.indexOf(R.id.royale), 0);
boutons.add(findViewById(R.id.quatreFromages)); boutons.add(findViewById(R.id.quatreFromages));
idIndexes.add(R.id.quatreFromages);
numCommands.add(idIndexes.indexOf(R.id.quatreFromages), 0);
boutons.add(findViewById(R.id.montagnarde)); boutons.add(findViewById(R.id.montagnarde));
idIndexes.add(R.id.montagnarde);
numCommands.add(idIndexes.indexOf(R.id.montagnarde), 0);
boutons.add(findViewById(R.id.raclette)); boutons.add(findViewById(R.id.raclette));
idIndexes.add(R.id.raclette);
numCommands.add(idIndexes.indexOf(R.id.raclette), 0);
boutons.add(findViewById(R.id.hawaii)); boutons.add(findViewById(R.id.hawaii));
idIndexes.add(R.id.hawaii);
numCommands.add(idIndexes.indexOf(R.id.hawaii), 0);
boutons.add(findViewById(R.id.pannaCotta)); boutons.add(findViewById(R.id.pannaCotta));
idIndexes.add(R.id.pannaCotta);
numCommands.add(idIndexes.indexOf(R.id.pannaCotta), 0);
boutons.add(findViewById(R.id.tiramisu)); boutons.add(findViewById(R.id.tiramisu));
idIndexes.add(R.id.tiramisu);
numCommands.add(idIndexes.indexOf(R.id.tiramisu), 0);
// On ajoute un listener sur chaque bouton
boutons.forEach((btn) -> btn.setOnClickListener(this)); boutons.forEach((btn) -> btn.setOnClickListener(this));
// On ajoute l'id de chaque bouton (normalement dans l'ordre
boutons.forEach((btn) -> idIndexes.add(btn.getId()));
// Et on ajoute le nom de chaque bouton (également normalement dans l'ordre)
boutons.forEach((btn) -> nomBoutons.add(btn.getText().toString()));
// On regarde s'il y a déjà eu une instance
if (savedInstanceState != null) {
// On récupère le nombre de commandes
numCommands = savedInstanceState.getIntegerArrayList("numCommands");
// Et on remet le nom avec les num de commandes aux boutons
boutons.forEach(this::setButtonText);
}else{
// On réserve les espaces pour les commandes
boutons.forEach((btn) -> numCommands.add(idIndexes.indexOf(btn.getId()), 0));
}
}
@Override
protected void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
outState.putIntegerArrayList("numCommands", numCommands);
} }
public void onClick(View v) { public void onClick(View v) {
try{ try{
Button bouton = boutons.get(idIndexes.indexOf(v.getId())); Button bouton = boutons.get(idIndexes.indexOf(v.getId()));
numCommands.set(idIndexes.indexOf(v.getId()), numCommands.get(idIndexes.indexOf(v.getId()))+1); numCommands.set(idIndexes.indexOf(v.getId()), numCommands.get(idIndexes.indexOf(v.getId()))+1);
bouton.setText(bouton.getText() + " : " + numCommands.get(idIndexes.indexOf(v.getId()))); setButtonText(bouton);
}catch ( Exception e ){ }catch ( Exception e ){
Log.e("Erreur", e.getMessage()); Log.e("Erreur", e.getMessage());
} }
} }
public void setButtonText(Button bouton){
if(numCommands.get(idIndexes.indexOf(bouton.getId())) == 0){
bouton.setText(nomBoutons.get(idIndexes.indexOf(bouton.getId())));
}else{
String titre = nomBoutons.get(idIndexes.indexOf(bouton.getId())) + " : " + numCommands.get(idIndexes.indexOf(bouton.getId()));
bouton.setText(titre);
}
}
} }
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment