Jump to content

Toast intempestif qui s'affiche


tomy29

Recommended Posts

Bonjour

J'ai une activity qui gère une list. Avec un contextMenu je décide de supprimer ou editer l'élément sur lequel j'ai cliqué.

Mon soucis c'est que j'ai un Toast (rappelant l'option du menu que j'ai sélectionné) qui s'affiche tout seul en meme temps que j'affiche mon Dialog d'édition de l'élément sélectionné. (cf pièce jointe)

Je ne comprends pas pourquoi.

Voici mon code :

Activity principale :

package com.formation.shoppinglist;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.ListView;
import android.widget.AdapterView.AdapterContextMenuInfo;

public class ManageList extends Activity {
   protected ListView lvIngr;
   ArrayList listIngr = new ArrayList();
   MyListAdapter adp;

   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.gestlist);
       lvIngr = (ListView)findViewById(R.id.lvIngredient);
       lvIngr.setOnCreateContextMenuListener(this);
       adp = new MyListAdapter(this, listIngr);
       lvIngr.setAdapter(adp);
   }

   @Override
   public boolean onCreateOptionsMenu(Menu menu){
       menu.add(0, 1000, 0, "Add");
       return super.onCreateOptionsMenu(menu);
   }

   public boolean onOptionsItemSelected(MenuItem item) {
       switch (item.getItemId()){
       case 1000 : 
               AddItemDialog itemDialog = new AddItemDialog(this,listIngr,adp,new String[0]);
               itemDialog.setTitle("Add an item");
               itemDialog.show();
               break;
       }
       return super.onOptionsItemSelected(item);
   }

   @Override
   public void onCreateContextMenu(ContextMenu menu, View v,
           ContextMenuInfo menuInfo) {
       menu.addSubMenu(0, 1000, 0, "Edit");
       menu.addSubMenu(0, 1001, 0, "Delete");
       super.onCreateContextMenu(menu, v, menuInfo);
   }

   @Override
   public boolean onContextItemSelected(MenuItem item) {
       AdapterContextMenuInfo acm = (AdapterContextMenuInfo) item.getMenuInfo();
       switch (item.getItemId()){
       case 1000 : Ingredient ingr = (Ingredient)listIngr.get(acm.position);
                   String[] ingrToEdit = new String[2];
                   ingrToEdit[0] = ingr.getLibelle();
                   ingrToEdit[1] = ingr.getQuantite();
                   AddItemDialog itemDialog = new AddItemDialog(this, listIngr,adp,ingrToEdit);
                   itemDialog.setTitle("Edit the item");
                   itemDialog.show();
                   break;
       case 1001 :    listIngr.remove(acm.position);
                   adp.notifyDataSetChanged();
                   break;
       }
       return super.onContextItemSelected(item);
   }
}

Dialog:

package com.formation.shoppinglist;

import java.util.ArrayList;
import java.util.Iterator;

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.EditText;
import android.widget.TextView;

public class AddItemDialog extends Dialog  implements OnClickListener{

   private Button  okButton;
   private String laquantite="";
   private String lingredient="";
   private ArrayList listIngr;
   private MyListAdapter adp;
   protected String[] detailIngr;
   public AddItemDialog(Context context, ArrayList _listIngr,
           MyListAdapter _adp, String[] _detailIngr) {
       super(context);
       this.listIngr = _listIngr;
       listIngr = _listIngr;
       adp = _adp;
       detailIngr = _detailIngr;
   }


   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
         Log.d("TestApp", "Dialog created");
           setContentView(R.layout.additem);            
           okButton = (Button) findViewById(R.id.btvalingre);
           okButton.setOnClickListener(this);

           TextView textquantite = (TextView) findViewById(R.id.etQuantite);
           TextView textingredient = (TextView) findViewById(R.id.etIngredient);
           if (detailIngr.length!=0){
               textquantite.setText(detailIngr[1]);
               textingredient.setText(detailIngr[0]);
           }
   }

   @Override
   public void onClick(View arg0) {
       laquantite = ((EditText) findViewById(R.id.etQuantite)).getText().toString();
       lingredient = ((EditText) findViewById(R.id.etIngredient)).getText().toString();
       Ingredient ingr = new Ingredient(lingredient, laquantite);
       if (detailIngr.length!=0){
           editIngredientInList(ingr);
       }else{
           listIngr.add(ingr);
       }

       adp.notifyDataSetChanged();
       dismiss();
   }

   @Override
   protected void onStart() {
       Log.i("Start", "Start de la dialogBox");
       super.onStart();
   }

   private void editIngredientInList(Ingredient ingr) {
       Iterator it= listIngr.iterator();
       boolean trouve = false;
       while(it.hasNext() && !trouve){
           Ingredient ingrToFind = it.next();
           if (ingr.getLibelle().equals(ingrToFind.getLibelle())){
               trouve = true;
               ingrToFind.setQuantite(ingr.getQuantite());
           }
       }
   }
}

Merci pour votre aide.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...