title: How do you delete an Item from a ListView from a dialog box class?
body: <p>I made my own dialog box class. This class has a button to delete an item from a listview(which is the main acctivity_main.xml). When I push the delete button the item does not get deleted. </p>
  
  <p>I have seen this topic <a href="https://stackoverflow.com/questions/4698386/android-how-to-remove-an-item-from-a-listview-and-arrayadapter">Android: how to remove an item from a listView and arrayAdapter</a>. It just appears the user does not know how to get the item index correctly, which I believe I have done correctly.</p>
  
  <p><a href="https://stackoverflow.com/questions/2558591/remove-listview-items-in-android">Remove ListView items in Android</a> This one is pretty close. But in my code I created my own dialog, this one is using a positive and negative button. I am passing variables between my dialog class and to the mainActivity. </p>
  
  <p>my onClickListener in OnCreate withing the MainActivity</p>
  
  <pre><code> mFoodDataAdapter = new FoodDataAdapter();
          final ListView listFoodData = (ListView) findViewById(R.id.listView);
          listFoodData.setAdapter(mFoodDataAdapter);
  
          //Handle clicks on the ListView
          listFoodData.setOnItemClickListener(new AdapterView.OnItemClickListener() {
              @Override
              public void onItemClick(AdapterView&lt;?&gt; adapter, View view, int whichItem, long id) {
  
                  FoodData tempFoodData = mFoodDataAdapter.getItem(whichItem);
                  //create a new dialog window
                  DialogShowFood dialog = new DialogShowFood();
                  // send in a reference to the note to be shown
                  dialog.sendFoodDataSelected(tempFoodData);
  
                  FoodDataAdapter adapter1 = new FoodDataAdapter();
  
                  /*this is where i send the data to the DialogShowFood.java*/
                  dialog.sendFoodDataAdapter(adapter1, whichItem);
  
                  // show the dialog window with the note in it
                  dialog.show(getFragmentManager(),"");
              }
          });
  </code></pre>
  
  <p>Here is my class for the dialog "DialogShowFood.java"</p>
  
  <pre><code>public class DialogShowFood extends DialogFragment {
  
      FoodData mFood;
      MainActivity.FoodDataAdapter mAdapter;
      int  mitemToDelete;
  
      @Override
      public Dialog onCreateDialog(Bundle savedInstanceState){
          AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
  
          LayoutInflater inflater = getActivity().getLayoutInflater();
          View dialogView = inflater.inflate(R.layout.dialog_show_food, null);
  
          Button btnDelete = (Button) dialogView.findViewById(R.id.btnDelete);
  
          builder.setView(dialogView).setMessage("Your food");
  
          /*this sends the item to delete to the adapter*/
          btnDelete.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  mAdapter.deleteFoodData(mitemToDelete);
                  dismiss();
              }
          });
          return builder.create();
      }
  
      /*this gets the data to delete from the MainActivity*/
      public void sendFoodDataAdapter(MainActivity.FoodDataAdapter adapter1, int whichItem) {
          mAdapter = adapter1;
          mitemToDelete = whichItem;
      }
  }
  </code></pre>
  
  <p>The function inside the adapter</p>
  
  <pre><code>  /*this is the function in the base adapter to delete the item*/
     public void deleteFoodData(int n){
              Toast.makeText(MainActivity.this,Integer.toString(n), Toast.LENGTH_SHORT).show();
              foodDataList.remove(n);
              notifyDataSetChanged();
          }
  </code></pre>
  
  <p>The Toast outputs the proper indexes of the item to delete, it just does not delete the item for some reason. </p>