title: Where should I call a method to remove items from a ListView if those items were added through user input from a Popup window?
body: <p>I have the following code in Android Studio. I'm able to open a popup whenever the &quot;+ New Semester&quot; button is clicked and a new semester is added to the Main Activity once the user types in the semester name in the popup and clicks on the &quot;Done&quot; button. Now, I want to implement a method that removes semesters that are no longer wanted. I made a method called <code>removeSemesters()</code> that I think that does what I want it to do, I haven't been able to test it because I'm not sure where I need to call that method in order to do what I want it to do. Any help would be appreciated! Thank you!</p>
  <pre><code>package com.example.gradecalculator;
  
  import android.app.Dialog;
  import android.content.Intent;
  import android.os.Bundle;
  import android.util.SparseBooleanArray;
  import android.view.View;
  import android.widget.AdapterView;
  import android.widget.ArrayAdapter;
  import android.widget.EditText;
  import android.widget.ImageButton;
  import android.widget.ListView;
  import android.widget.TextView;
  import android.widget.Toast;
  
  import androidx.appcompat.app.AppCompatActivity;
  
  import java.util.ArrayList;
  
  public class MainActivity extends AppCompatActivity {
  
      // Private Fields
      private Dialog d;
      private EditText semesterName;
      private ListView semesterList;
      private ArrayList&lt;String&gt; semesterArray = new ArrayList&lt;String&gt;();
      private ArrayAdapter&lt;String&gt; semesterAdapter;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          d = new Dialog(this);
      }
  
      // When user clicks on &quot;+ New Semester&quot; button open a popup where the user is prompted to
      // type in the Semester Name and when &quot;Done&quot; is clicked the new semester appears in the Main
      // Activity
      public void newSemesterPopup(View v) {
          TextView closePopup;
          ImageButton doneButton;
  
          d.setContentView(R.layout.new_semester_popup);
  
          semesterName = (EditText) d.findViewById(R.id.editTextSemesterName);
          semesterList = (ListView) findViewById(R.id.semesterList);
  
          doneButton = (ImageButton) d.findViewById(R.id.doneButton);
          doneButton.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  addSemesters();
              }
          });
  
          closePopup = (TextView) d.findViewById(R.id.exitButton);
          closePopup.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  d.dismiss();
              }
          });
          d.show();
      }
  
      // Adds semesters to Main Activity
      public void addSemesters() {
          String getSemesterName = semesterName.getText().toString();
  
          if(semesterArray.contains(getSemesterName)) {
              Toast.makeText(getBaseContext(), &quot;Semester Name already exists.&quot;, Toast.LENGTH_LONG).show();
          }
          else if(getSemesterName == null || getSemesterName.trim().equals(&quot;&quot;)) {
              Toast.makeText(getBaseContext(), &quot;Cannot add empty Semester Name&quot;, Toast.LENGTH_LONG).show();
          }
          else {
              semesterAdapter = new ArrayAdapter&lt;String&gt;(MainActivity.this, android.R.layout.simple_list_item_multiple_choice, semesterArray);
              semesterList.setAdapter(semesterAdapter);
              semesterAdapter.add(getSemesterName);
              d.dismiss();
          }
      }
  
      // Removes unwanted semesters from Main Activity
      public void removeSemesters() {
          semesterList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
              @Override
              public boolean onItemLongClick(AdapterView&lt;?&gt; parent, View view, int position, long id) {
  
                  SparseBooleanArray positionChecker = semesterList.getCheckedItemPositions();
                  int counter = semesterList.getCount();
  
                  for(int i = counter - 1; i &gt; 1; i--) {
                      if(positionChecker.get(i)) {
                          semesterAdapter.remove(semesterArray.get(i));
                          Toast.makeText(MainActivity.this, &quot;Semester deleted successfully.&quot;, Toast.LENGTH_LONG).show();
                      }
                  }
                  positionChecker.clear();
                  semesterAdapter.notifyDataSetChanged();
  
                  return false;
              }
          });
      }
  
      // Opens Main Activity
      public void openMainActivity() {
          Intent main = new Intent(this, MainActivity.class);
          startActivity(main);
      }
  }
  </code></pre>