Dans L’étape précédente “Développez une application pour android étape 20 (Appui long sous android pour supprimer des ingrédients)” , sur un appui long j’ouvrais une boite de dialogue pour confirmer la suppression de l’ingrédient sélectionné.
Dans cette étape, je vais utiliser le click simple pour modifier l’ingrédient sélectionné. J’ouvre une boite de dialogue personnalisée avec les champs déjà remplis.
D’abord le fichier xml de mon formulaire
Je crée ma classe Myclassdialogmodif.java (une nouvelle boite de saisie).
public class Myclassdialogmodif extends Dialog implements OnClickListener{
private Button okButton;
private Button cancelButton;
private Context lecontext;
private ReadyListener readyListener;
private String lunite="";//unité
private String laquantite="";//quantité
private String lingredient="";// nom de l'ingredient
private int lapositon=0;//attribut pour la position de l'ingredient dans ma liste
public interface ReadyListener {
public void ready(String sUnite,String sQuantite,String sIngredient,int position);
}
Dans mon constructeur, je passe les valeurs à renseigner dans les champs
public Myclassdialogmodif(Context context, ReadyListener readyListener,
String unite,String squantite,String ingredient,int position) {
super(context);
lecontext = context;
this.readyListener = readyListener;
this.lunite=unite;//l'unité de mesure
this.laquantite=squantite;// la quantité
this.lingredient=ingredient;//nom de l'ingredient
this.lapositon=position;//la postion dans la listview
}
Au lancement de ma fenêtre de saisie, je renseigne les champs.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("TestApp", "Dialog created");
setContentView(R.layout.choixingredient);
okButton = (Button) findViewById(R.id.btvalingre);
cancelButton = (Button) findViewById(R.id.btquitter);
okButton.setOnClickListener(this);
cancelButton.setOnClickListener(this);
//récupération de mes différent champs de mon xml
TextView textmesure = (TextView) findViewById(R.id.valunite);
TextView textquantite = (TextView) findViewById(R.id.valquante);
TextView textingredient = (TextView) findViewById(R.id.valingredient);
//renseignement de mes champs dans la boite de saisie
textmesure.setText(lunite);
textquantite.setText(laquantite);
textingredient.setText(lingredient);
}
La suite du ma classe et la même que celle de l’étape 15 “Développez une application pour android étape 15 (Rajouter une listview dans mon formulaire pour afficher les ingrédients)“.
Tout le code.
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class Myclassdialogmodif extends Dialog implements OnClickListener{
private Button okButton;
private Button cancelButton;
private Context lecontext;
private ReadyListener readyListener;
private String lunite="";
private String laquantite="";
private String lingredient="";
private int lapositon=0;
public interface ReadyListener {
public void ready(String sUnite,String sQuantite,String sIngredient,int position);
}
public Myclassdialogmodif(Context context, ReadyListener readyListener,
String unite,String squantite,String ingredient,int position) {
super(context);
lecontext = context;
this.readyListener = readyListener;
this.lunite=unite;
this.laquantite=squantite;
this.lingredient=ingredient;
this.lapositon=position;
}
/**
* @see android.app.Dialog#onCreate(android.os.Bundle)
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("TestApp", "Dialog created");
setContentView(R.layout.choixingredient);
okButton = (Button) findViewById(R.id.btvalingre);
cancelButton = (Button) findViewById(R.id.btquitter);
okButton.setOnClickListener(this);
cancelButton.setOnClickListener(this);
TextView textmesure = (TextView) findViewById(R.id.valunite);
TextView textquantite = (TextView) findViewById(R.id.valquante);
TextView textingredient = (TextView) findViewById(R.id.valingredient);
textmesure.setText(lunite);
textquantite.setText(laquantite);
textingredient.setText(lingredient);
}
public void onClick(View view) {
switch (view.getId()) {
case R.id.btquitter:
dismiss();
break;
case R.id.btvalingre:
try{
TextView textmesure = (TextView) findViewById(R.id.valunite);
TextView textquantite = (TextView) findViewById(R.id.valquante);
TextView textingredient = (TextView) findViewById(R.id.valingredient);
String sRetour =textquantite.getText()+","+textmesure.getText()+","+textingredient.getText();
String sUnite = textmesure.getText()+"";
String sQuantite = textquantite.getText()+"";
String sIngredient = textingredient.getText()+"";
//appelle de la methode readyListener.ready et on passe en valeur le formatage
readyListener.ready(sUnite,sQuantite,sIngredient,lapositon);
//String u="kk";
}catch(Exception e){
TextView textbastmp2 = (TextView) findViewById(R.id.ingredient);
textbastmp2.setText(e.toString());
}
break;
}
}
}
Dans ma class EnregRecette.java, je rajoute la méthode onItemClick pour le click simple.
public void onItemClick(AdapterView parent, View v, int position, long id) {
//***********suppression des informations dans mon tableau********
//création de mon objet en passant les valeurs des champs à renseigner ainsi qui la position dans ma liste
Myclassdialogmodif dialog = new Myclassdialogmodif(v.getContext(),
new OnReadyListenerModif(),aTableauIngredient.get(position)[0],
aTableauIngredient.get(position)[1],aTableauIngredient.get(position)[2]
,position);
dialog.setTitle("Modifier l'ingredient");
dialog.show();
}
Je rajoute également la class OnReadyListenerModif implements Myclassdialogmodif.ReadyListener.
Implementant la méthode ready.
private class OnReadyListenerModif implements Myclassdialogmodif.ReadyListener
{
public void ready(String sUnite,String sQuantite,String sIngredient,int position) {
String[] sNewtext=new String[]{sUnite,sQuantite,sIngredient};
String sNewtextaff="";
// modification de la position dans notre tableau d'ingrédient
aTableauIngredient.set(position, sNewtext);
//modification de l'aaffichage dans notre listwiex
aTableauIngredientaff.set(position,sQuantite+" "+sUnite+" "+sIngredient);
//rafraichissement de ma listview
mRecetteList = (ListView) findViewById(android.R.id.list);
final ArrayAdapter adapter2 = new ArrayAdapter(getBaseContext(),
android.R.layout.simple_list_item_1,aTableauIngredientaff);
mRecetteList.setAdapter(adapter2);
}
}
tout le code de ma class EnregRecette.java
public class EnregRecette extends Activity implements AdapterView.OnItemClickListener, View.OnClickListener{
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.setOnItemClickListener(this);
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
{
public void ready(String sUnite,String sQuantite,String sIngredient) {
String[] sNewtext=new String[]{sUnite,sQuantite,sIngredient};
String sNewtextaff="";
aTableauIngredient.add(sNewtext);
aTableauIngredientaff.add(sQuantite+" "+sUnite+" "+sIngredient);
mRecetteList = (ListView) findViewById(android.R.id.list);
final ArrayAdapter adapter2 = new ArrayAdapter(getBaseContext(),
android.R.layout.simple_list_item_1,aTableauIngredientaff);
mRecetteList.setAdapter(adapter2);
//textbastmp2.setText(sNewtextaff);
}
}
private class OnReadyListenerModif implements Myclassdialogmodif.ReadyListener
{
public void ready(String sUnite,String sQuantite,String sIngredient,int position) {
String[] sNewtext=new String[]{sUnite,sQuantite,sIngredient};
String sNewtextaff="";
aTableauIngredient.set(position, sNewtext);
aTableauIngredientaff.set(position,sQuantite+" "+sUnite+" "+sIngredient);
mRecetteList = (ListView) findViewById(android.R.id.list);
final ArrayAdapter adapter2 = new ArrayAdapter(getBaseContext(),
android.R.layout.simple_list_item_1,aTableauIngredientaff);
mRecetteList.setAdapter(adapter2);
}
}
// action sur le clik sur un item de ma listview
public void onItemClick(AdapterView parent, View v, int position, long id) {
Myclassdialogmodif dialog = new Myclassdialogmodif(v.getContext(),
new OnReadyListenerModif(),aTableauIngredient.get(position)[0],
aTableauIngredient.get(position)[1],aTableauIngredient.get(position)[2]
,position);
dialog.setTitle("Modifier l'ingredient");
dialog.show();
}
public void onClick(View v) {
int u=0;
}
}
Code source de l’application 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