Dans cette étape 20 je vais utiliser une des actions que je trouve très intéressante sous android “l’appui long”.
J’utiliserai cette action pendant l’enregistrement d’une recette.
Quand j’aurais un appui long sur un item de ma liste des ingrédients, j’ouvrirai une demande de confirmation pour le supprimer.
L’utilisation de cette action “appui long” nous laisse également la possibilité sur un appui court de faire une autre action, ici la modification des valeurs de notre ingrédient (unité de mesure, quantité, libellé) mais ça c’est pour le prochain tuto.
Pour utiliser l’appui long sur une listview il faut tout simplement implement la méthode setOnItemLongClickListener de la listeview.
Je modifie donc ma classe EnregRecette en ajoutant donc:
//ma fonction appui long sur ma listview
mRecetteList.setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView parent, View v, int position, long id) {
ilaposition=position;
//création d'un boite de dialogue pour confirmer le choix
new AlertDialog.Builder(EnregRecette.this)
.setTitle("Confirmation")
.setMessage("Voulez vous supprimer cet ingredient de la liste")
.setPositiveButton("OUI", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int whichButton)
{
aTableauIngredientaff.remove(ilaposition);
aTableauIngredient.remove(ilaposition);
mRecetteList = (ListView) findViewById(android.R.id.list);
final ArrayAdapter adapter2 = new ArrayAdapter(getBaseContext(),
android.R.layout.simple_list_item_1,aTableauIngredientaff);
mRecetteList.setAdapter(adapter2);
}
})
.setNegativeButton("NON", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
/* User clicked Cancel so do some stuff */
}
})
.show();
return true;
}
Le code entier donne donc
public class EnregRecette extends Activity {
private ListView mRecetteList;
private Context lecontext;
private final ArrayList aTableauIngredient= new ArrayList();
private final ArrayList aTableauIngredientaff= new ArrayList();
private BDAcces objBd=new BDAcces(this);
private int ilaposition=0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
lecontext =this;
this.setContentView(R.layout.main);
Button btEnregRecette = (Button) findViewById(R.id.button);
Button btEnregajcon = (Button) findViewById(R.id.ajcondiment);
final TextView texthaut = (TextView) findViewById(R.id.entry);
final TextView textbas = (TextView) findViewById(R.id.entry3);
mRecetteList = (ListView) findViewById(android.R.id.list);
final ArrayAdapter adapter2 = new ArrayAdapter(getBaseContext(),
android.R.layout.simple_list_item_1,aTableauIngredientaff);
mRecetteList.setAdapter(adapter2);
//final TextView textmilieu = (TextView) findViewById(R.id.entry2);
mRecetteList.setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView parent, View v, int position, long id) {
ilaposition=position;
new AlertDialog.Builder(EnregRecette.this)
.setTitle("Confirmation")
.setMessage("Voulez vous supprimer cet ingredient de la liste")
.setPositiveButton("OUI", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int whichButton)
{
aTableauIngredientaff.remove(ilaposition);
aTableauIngredient.remove(ilaposition);
mRecetteList = (ListView) findViewById(android.R.id.list);
final ArrayAdapter adapter2 = new ArrayAdapter(getBaseContext(),
android.R.layout.simple_list_item_1,aTableauIngredientaff);
mRecetteList.setAdapter(adapter2);
}
})
.setNegativeButton("NON", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
/* User clicked Cancel so do some stuff */
}
})
.show();
return true;
}
});
//this.setContentView(R.layout.liste_recette);
btEnregRecette.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
try{
//textbas.setText(texthaut.getText());
String sNomRecette = texthaut.getText().toString();
String sContenuRecette = textbas.getText().toString();
objBd.open();
//texthaut.setText(objBd.renvoi_requete());
long bresult = objBd.insertRecette(sNomRecette, sContenuRecette,aTableauIngredient);
String resultsring = new Long(bresult).toString() ;
textbas.setText(resultsring);
//textmilieu.setText(sContenuRecette);
objBd.close();
}
catch (Exception e) {
textbas.setText("erreur "+e.getMessage());
}
}
});
btEnregajcon.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
//View v v.getContext()
Myclassdialog dialog = new Myclassdialog(v.getContext(),
new OnReadyListener());
dialog.setTitle("Enregistrement ingredient");
dialog.show();
}
});
}
private class OnReadyListener implements Myclassdialog.ReadyListener,
AdapterView.OnItemClickListener, View.OnClickListener{
public void ready(String sUnite,String sQuantite,String sIngredient) {
String[] sNewtext=new String[]{sUnite,sQuantite,sIngredient};
String sNewtextaff="";
//tableau temporaire des informations
aTableauIngredient.add(sNewtext);
//tableau temporaire de l'affichage
aTableauIngredientaff.add(sQuantite+" "+sUnite+" "+sIngredient);
/*int iTailletab = aTableauIngredient.size();
for(int i=0;i adapter2 = new ArrayAdapter(getBaseContext(),
android.R.layout.simple_list_item_1,aTableauIngredientaff);
mRecetteList.setAdapter(adapter2);
mRecetteList.setOnItemClickListener(this);
//TextView textbastmp2 = (TextView) findViewById(R.id.entry2);
//textbastmp2.setText(sNewtextaff);
}
// action sur le clik sur un item de ma listview
public void onItemClick(AdapterView parent, View v, int position, long id) {
//***********suppression des informations dans mon tableau********
aTableauIngredientaff.remove(position);
aTableauIngredient.remove(position);
mRecetteList = (ListView) findViewById(android.R.id.list);
final ArrayAdapter adapter2 = new ArrayAdapter(getBaseContext(),
android.R.layout.simple_list_item_1,aTableauIngredientaff);
mRecetteList.setAdapter(adapter2);
mRecetteList.setOnItemClickListener(this);
}
public void onClick(View v) {
int u=0;
}
}
}
code source ici
Il est temps de mettre à profit les tutoriels d’Android France:
Passez à la vitesse supérieure et investissez quelques dizaines d’euros pour acquérir les connaissances qui vous feront gagner de l’argent avec vos applications rendez-vous sur notre boutique Android-france pour ces formations en vidéo