• Home
  • Youzik
  • Papystreaming
  • Darknet
  • Libertyland
  • Cacaoweb
  • GG maps
  • Yggtorrent
  • Zone téléchargement

Grimag

  • Accueil
  • Catégories
    • Smartphone
    • High Tech
    • Business
    • Web
    • Appli Jeux Vidéo
    • Shopping
  • Android c’est quoi ?
  • Nous Contacter
  • Nos comparatifs
    • Drone avec caméra
    • Clavier d’ordinateur
    • Onduleur
    • GPS
    • Répéteur WiFi

Développez une application pour android étape 20 (Appui long sous android pour supprimer des ingrédients)



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;
        }

   } 

}

Résultat
etape201 etape202 etape203

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

Juil 20, 2009Gnakouri Danon


L'application Android du site participatif d'information Rue89 disponibleNouvelle mise à jour du SDK Android 1.5 r3
Gnakouri Danon

Co-fondateur du site Android france, senior lead developper, passionné de bière et de cigare cubain

20 juillet 2009 Developpement, Tuto développementandroidn, listview, tuto947

2023 © Android-France