Problème avec mon code Android

LapindeTerraria -  
BunoCS Messages postés 15952 Date d'inscription   Statut Modérateur Dernière intervention   -
Bonjour,

J'aimerais ajouter une fonctionnalité à une application android.

L'application permet d'y mettre des informations sur nous comme le poids, la taille et âge pour obtenir l'IMG.

Après l'application montre les mesures dans une liste.

On peut supprimer l'une des mesures, en appuyant sur un bouton "btnListSuppr" (une croix qui est en début de ligne)

Et je voudrais juste ajouter une étape de confirmation du genre "Vous voulez vraiment supprimer la ligne ?" où il faut choisir "OUI", pour supprimer la ligne.

J'ai trouvé un code que j'ai mis plus bas sur un autre site mais je n'arrive pas à l'ajouter.

Voici, mon code :
Et un autre que j'ai trouvé sur un autre site,
package com.example.coach.vue;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.TextView;
import com.example.coach.R;
import com.example.coach.controleur.Controle;
import com.example.coach.modele.Profil;
import com.example.coach.outils.MesOutils;
import java.util.ArrayList;
public class HistoListAdapter extends BaseAdapter {
 private ArrayList<Profil> lesProfils;
 private LayoutInflater inflater;
 private Context context;
 /**
 * Constructeur
 * @param context
 * @param lesProfils
 */
 public HistoListAdapter(Context context, ArrayList<Profil> lesProfils){
 this.context = context;
 this.lesProfils = lesProfils;
 this.inflater = LayoutInflater.from(context);
 }
 /**
 * How many items are in the data set represented by this Adapter.
 *
 * @return Count of items.
 */
 @Override
 public int getCount(){
 return lesProfils.size();
 }
 /**
 * Get the data item associated with the specified position in the data set.
 *
 * @param position Position of the item whose data we want within the adapter's
 * data set.
 * @return The data at the specified position.
 */
 @Override
 public Object getItem(int position) {
 return lesProfils.get(position);
 }
 /**
 * Get the row id associated with the specified position in the list.
 *
 * @param position The position of the item within the adapter's data 
 * set whose row id we want.
 * @return The id of the item at the specified position.
 */
 *
 * @param position Position of the item whose data we want within the adapter's
 * data set.
 * @return The data at the specified position.
 */
 @Override
 public Object getItem(int position) {
 return lesProfils.get(position);
 }
 /**
 * Get the row id associated with the specified position in the list.
 *
 * @param position The position of the item within the adapter's data 
 * set whose row id we want.
 * @return The id of the item at the specified position.
 */
 @Override
 public long getItemId(int position) {
 return position;
 }
 /**
 * Get a View that displays the data at the specified position in the data set.
 * @param position The position of the item within the adapter's data set 
 * of the item whose view we want.
 * @param convertView The old view to reuse, if possible.
 * @param parent The parent that this view will eventually be attached to
 * @return A View corresponding to the data at the specified position.
 */
 @Override
 public View getView(int position, View convertView, ViewGroup parent) {
 ViewProperties viewProperties;
 if(convertView == null){
 viewProperties = new ViewProperties();
 convertView = inflater.inflate(R.layout.layout_liste_histo, null);
 viewProperties.txtListDate = (TextView)convertView.findViewById(R.id.txtListDate);
 viewProperties.txtListIMG = (TextView)convertView.findViewById(R.id.txtListIMG);
 viewProperties.btnListSuppr = (ImageButton) convertView.findViewById(R.id.btnListSuppr);
 convertView.setTag(viewProperties) ;
 }else{
 viewProperties = (ViewProperties)convertView.getTag();
 }
 viewProperties.txtListDate.setText(MesOutils.convertDateToString(lesProfils.get(position).
getDateMesure()));
 viewProperties.txtListIMG.setText(MesOutils.format2Decimal(lesProfils.get(position)
.getImg()));
 viewProperties.btnListSuppr.setTag(position);
 viewProperties.btnListSuppr.setOnClickListener(new View.OnClickListener(){
 @Override
 public void onClick(View v) {
 int indice = (int)v.getTag();
 Controle controle = Controle.getInstance(null);
 controle.delProfil(lesProfils.get(indice));
 notifyDataSetChanged() ;
 }
 });
 viewProperties.txtListDate.setTag(position);
 viewProperties.txtListDate.setOnClickListener(new View.OnClickListener(){
 @Override
 public void onClick(View v) {
 int indice = (int)v.getTag();
 ((HistoActivity)context).afficheProfil(lesProfils.get(indice));
 }
 });
 viewProperties.txtListIMG.setTag(position);
 viewProperties.txtListIMG.setOnClickListener(new View.OnClickListener(){
 @Override
 public void onClick(View v) {
 int indice = (int)v.getTag();
 ((HistoActivity)context).afficheProfil(lesProfils.get(indice));
 }
 });
 return convertView;
 }
 /**
 * Objets graphiques
 */
 private class ViewProperties{
 TextView txtListDate;
 TextView txtListIMG;
 ImageButton btnListSuppr;
 }
}
A voir également:

2 réponses

LapindeTerraria
 
J'ai oublié de rajouter l'exemple de code que j'ai trouvé sur un site.

Le voici:

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Confirm");
builder.setMessage("Are you sure?");
builder.setPositiveButton("YES", new DialogInterface.OnClickListener(){
 public void onClick(DialogInterface dialog, int which) {
 // Do nothing but close the dialog
 dialog.dismiss();
 }
});
builder.setNegativeButton("NO", new DialogInterface.OnClickListener(){
 @Override
 public void onClick(DialogInterface dialog, int which) {
 // Do nothing
 dialog.dismiss();
 }
});
AlertDialog alert = builder.create();
alert.show();
0
BunoCS Messages postés 15952 Date d'inscription   Statut Modérateur Dernière intervention   3 918
 
Hello,

L'
AlertDialog
est exactement le composant à utiliser. Maintenant, il faut mettre ce code dans le OnClickListener de ton bouton, et le code précédemment dans ce listener, il faut le mettre dans la callback "YES" de l'AlertDialog
0