title: Deleting ListView item from list and SharedPreferences
body: <p>I've looked around, and there are some similar questions, but none with a working solution. </p>
  
  <p>I've created a simple ListView to take input from an EditText and display it in a list on button click. If a list item is clicked, I have a dialog popup and ask for confirmation to delete. When "yes" is clicked, the item appears to delete. Though if I then try to delete another item, that item will delete, but the previous item will reappear in its place. I have no idea how to solve this problem. From my limited understanding it seems as though the item is never deleted from SharedPreferences, and when I load them again on the next "onClick", the old item appears again. Though I've tried clearing SharedPreferences and then saving them after deleting, which did not work.</p>
  
  <pre><code>package com.example.send2omni;
  
  import android.app.Activity;
  import android.app.AlertDialog;
  import android.content.DialogInterface;
  import android.content.SharedPreferences;
  import android.os.Bundle;
  import android.preference.PreferenceManager;
  import android.text.AndroidCharacter;
  import android.util.Log;
  import android.view.View;
  import android.widget.AdapterView;
  import android.widget.ArrayAdapter;
  import android.widget.Button;
  import android.widget.EditText;
  import android.widget.ListView;
  import android.widget.TextView;
  import java.util.ArrayList;
  import java.util.Arrays;
  import java.util.List;
  
  
  public class ContextsList extends Activity {
  
  public static final String CONTEXTS = "contexts_list";
  
  EditText display;
  ListView lv;
  public ArrayAdapter&lt;String&gt; adapter;
  Button addButton;
  String temp_appender;
  String appender = "";
  List&lt;String&gt; dataset;
  String[] splitup;
  public String items;
  ArrayList&lt;String&gt; itemsarraylist;
  
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.contexts_list);
  
      display = (EditText) findViewById(R.id.editText);
      lv = (ListView) findViewById(R.id.listView);
      addButton = (Button) findViewById(R.id.bAdd);
      LoadPreferences();
      lv.setAdapter(adapter);
      LoadPreferences();
      adapter = new ArrayAdapter&lt;String&gt;(this, android.R.layout.simple_list_item_1);
  
      addButton.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
  
              appender = LoadPreferences();
              if(display.getText().toString() != null){
                  temp_appender = display.getText().toString();
                  String string_to_split = appender + "," + temp_appender;
                  List&lt;String&gt; items = Arrays.asList(string_to_split.split(","));
                  adapter = new ArrayAdapter&lt;String&gt;
  (getApplicationContext(), android.R.layout.simple_list_item_1, items);
  
                  lv.setAdapter(adapter);
                  adapter.notifyDataSetChanged();
                  SavePreferences("LISTS", string_to_split, null);
  
                  LoadPreferences();
              }
  
  
              display.setText("");
          }
      });
  
      lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
          @Override
          public void onItemClick(AdapterView&lt;?&gt; arg0, View arg1, int arg2, long arg3) {
  
              deleteItem(arg1);
          }
      });
  
  }
  
  protected void deleteItem(final View arg1)
  {
      AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
      dialogBuilder.setTitle("Delete");
      dialogBuilder.setMessage
  ("Do you want to delete \"" + ((TextView) arg1).getText() + "\"?");
      dialogBuilder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
          @Override
          public void onClick(DialogInterface dialog, int which) {
  
  
              String splitit = LoadPreferences();
              List&lt;String&gt; listit = Arrays.asList(splitit.split(","));
              ArrayList&lt;String&gt; itemsarraylist = new ArrayList&lt;String&gt;();
              for(int i = 0; i &lt;listit.size(); i++){
                  itemsarraylist.add(i, listit.get(i));
              }
  
  
              try {
                  adapter.remove(((TextView) arg1).getText().toString());
                  adapter.notifyDataSetChanged();
                  itemsarraylist.remove(((TextView) arg1).getText().toString());
  
  
                  try{
                      SavePreferences("LISTS", null, itemsarraylist);
  
  
                  }catch(Exception e){
  
                  }
              } catch (Exception e) {
  
  
                  Log.e("remove", "failed");
              }
  
  
  
          }
  
  
      });
      dialogBuilder.setNeutralButton("No", new DialogInterface.OnClickListener() {
          @Override
          public void onClick(DialogInterface dialog, int which) {
  
          }
      });
      AlertDialog deleteDialog = dialogBuilder.create();
      deleteDialog.show();
  }
  
  
  protected void SavePreferences(String key, String value, ArrayList&lt;String&gt; opt) {
      // TODO Auto-generated method stub
      SharedPreferences data = PreferenceManager.getDefaultSharedPreferences(this);
  
      if (opt != null){
          for (int i = 0 ; i &lt;= opt.size(); i++)
              value += opt.get(i) + ",";
      }
  
      SharedPreferences.Editor editor = data.edit();
      editor.putString(key, value);
      editor.commit();
  
  
  }
  
  protected String LoadPreferences(){
      SharedPreferences data = PreferenceManager.getDefaultSharedPreferences(this);
      String dataSet = data.getString("LISTS", "");
      dataset = Arrays.asList(dataSet.split(","));
      splitup = dataSet.split(",");
  
  
  
      List&lt;String&gt; items = Arrays.asList(dataSet.split(","));
      ArrayList&lt;String&gt; itemsarraylist = new ArrayList&lt;String&gt;();
      for(int i = 0; i &lt;items.size(); i++){
          itemsarraylist.add(i, items.get(i));
      }
      adapter = new ArrayAdapter&lt;String&gt;
      (this, android.R.layout.simple_list_item_1,  itemsarraylist);
  
      lv.setAdapter(adapter);
      adapter.notifyDataSetChanged();
  
      return dataSet;
  
  }
  
  }
  </code></pre>
  
  <p>I've included all the code from the class, just in case you have the time to try it out in your IDE, and because the working part may be useful to other beginners.</p>
  
  <p>I can't express how grateful I'd be if anyone could help me fix this. It's pretty much the last step I have to take before completing my first app, and I'm really not looking forward to having to learn SQLite just to save a list of strings.</p>
  
  <p>Thanks for your time everyone..</p>