title: OnItemLongClickListener only deletes SD card file once, then no more
body: <p>I have an <code>ArrayList&lt;&gt;</code> in a <code>ListView</code>, so I need an adapter. The list items are saved to file in another activity, and load from my SD card once my <code>CuteCollection.java</code> activity runs. I put an <code>OnItemLongClickListener</code> on each list item, so that when I click it, the item is not only removed from the list (the adapter), but also the SD card. However, I need to NOT have the full file name, when displaying the title in my <code>ListView</code>, so I used <code>substring</code> to take off the file extension, which works well. But then a new problem developed... my click listener wouldn't delete the file from my SD card (since it needs the full file name with extension to know which one to delete). To work around that, I used the title of the file, and through <code>StringBuilder</code>, added the <code>.txt</code> extension back onto the string. But now it will only delete one item from file, but no more. Before I messed with removing the extension, it worked fine. I could delete any list item, by long-clicking it and pressing ok.</p>
  
  <p>Anyone know why it only works once, then doesn't delete anymore? It will remove any item from the list (the adapter), but when I rerun the activity, the old ones load again (except the first one I deleted). This tells me the SD card only had one deleted from it. I need to be able to delete as many as I want. Thanks for your help.</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>I logged both <code>singleFile</code> and <code>sb.toString()</code> and it looks like the <code>sb.toString()</code> has the first long-click file name on the 2nd long-click as well. The <code>singleFile</code> file name has 2 unique file names for both clicks, as it should, since there were currently 2 files in the list.</p>
  
  <p><strong>Before click:</strong></p>
  
  <pre><code>Value of singleFile is: /storage/emulated/0/Documents/CuteWords/Poem_40.txt
  Value of singleFile is: /storage/emulated/0/Documents/CuteWords/Poem_89.txt
  </code></pre>
  
  <p><strong>1st click:</strong></p>
  
  <pre><code>Value of sb.toString() is: Poem_89.txt
  </code></pre>
  
  <p><strong>2nd click:</strong></p>
  
  <pre><code>Value of sb.toString() is: Poem_89.txt
  </code></pre>
  
  <p>My fragment below:</p>
  
  <p><strong>TextTab.java</strong> This is a single tab in my <strong>CuteCollection.java</strong> activity.</p>
  
  <pre><code>package org.azurespot.cutecollection;
  
  import android.app.AlertDialog;
  import android.content.DialogInterface;
  import android.os.Bundle;
  import android.os.Environment;
  import android.support.v4.app.Fragment;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.AdapterView;
  import android.widget.AdapterView.OnItemClickListener;
  import android.widget.AdapterView.OnItemLongClickListener;
  import android.widget.ListView;
  
  import org.azurespot.R;
  
  import java.io.BufferedReader;
  import java.io.File;
  import java.io.FileReader;
  import java.io.IOException;
  import java.util.ArrayList;
  
  
  /**
   * Created by mizu on 2/8/15.
   */
  public class TextTab extends Fragment {
  
      private ArrayList&lt;PoemListItem&gt; poems = new ArrayList&lt;&gt;();
      private ListViewPoemAdapter adapter;
      private ListView listView;
      String[] allSDCardFiles = null;
      StringBuilder text;
      PoemListItem wordsFromFile;
      StringBuilder sb = new StringBuilder();
  
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                               Bundle savedInstanceState) {
  
          // Inflate the layout for this fragment
          View v = inflater.inflate(R.layout.text_tab, container, false);
          adapter = new ListViewPoemAdapter(getActivity(), poems);
          // Attach the adapter to a ListView
          listView = (ListView) v.findViewById(R.id.text_list_view);
          listView.setAdapter(adapter);
  
          // read contents of SD card
          loadSDCard();
  
          // add the default icon/lines remaining to ArrayList, if less than 24 files on SD card
          for (int i = 0; i &lt; (24 - allSDCardFiles.length); i++) {
              PoemListItem sampleItem = new PoemListItem(" ", " ");
              adapter.add(sampleItem);
              adapter.notifyDataSetChanged();
              i++;
          }
  
  
          setupListViewListener();
  
          return v;
  
      }
  
      private void loadSDCard(){
          // gets directory CuteWords from sd card
          File baseDir = Environment.getExternalStorageDirectory();
          File cuteWordsDir = new File(baseDir, "/Documents/CuteWords");
          // lists all files in CuteWords, loads in Files[] array
          File[] files = cuteWordsDir.listFiles();
  
  
          for (File singleFile : files) {
              //Read text from file, put each line into StringBuilder
              text = new StringBuilder();
              try {
                  BufferedReader br = new BufferedReader(new FileReader(singleFile));
                  String line;
  
                  while ((line = br.readLine()) != null) {
                      text.append(line);
                      text.append('\n');
  
                      // get full file name with ext. and text in file
                      wordsFromFile = new PoemListItem(singleFile.getName(), text.toString());
  
                      // get only name from title (not file extension)
                      String withExt = wordsFromFile.getTitle();
                      String justTitle = withExt.substring(0, withExt.length() - 4);
                      wordsFromFile.setTitle(justTitle);
  
                      adapter.add(wordsFromFile);
                      adapter.notifyDataSetChanged();
  
                  }
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
          // get number of files in CuteWords directory
          allSDCardFiles =  new String[files.length];
      }
  
  
      // so you can edit any of the list items
      private void setupListViewListener() {
          listView.setOnItemClickListener(new OnItemClickListener() {
              @Override
              public void onItemClick(AdapterView&lt;?&gt; adapter,
                                          View item, int pos, long id){
  
  
              }
          });
  
          // to delete an item
          listView.setOnItemLongClickListener(new OnItemLongClickListener() {
              @Override
              public boolean onItemLongClick(AdapterView&lt;?&gt; aView, View item,
                                             final int pos, long id) {
  
                  new AlertDialog.Builder(getActivity())
                          .setTitle("Delete")
                          .setMessage("Delete these cute words?")
                          .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                              public void onClick(DialogInterface dialog, int which) {
                                  // delete from adapter
                                  poems.remove(pos);
                                  adapter.notifyDataSetChanged();
                                  // add extension back on file name
                                  sb.append(wordsFromFile.getTitle() + ".txt");
                                  // get file name then delete it
                                  File file = new File(Environment.getExternalStorageDirectory(),
                                          "/Documents/CuteWords/" + sb.toString());
                                  file.delete();
                              }
                          })
                          .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
                              public void onClick(DialogInterface dialog, int which) {
  
                                  // do nothing
  
                              }
                          })
                          .setIcon(android.R.drawable.ic_dialog_alert)
                          .show();
       return true;
              }
          });
      }
  }
  </code></pre>