title: How to delete a row in Sqlite database in Android application by user at listview
body: <p>I made an Android application with a SQLite database. I want the user to be able to select a row of the database in a listview and be able to delete that specific row.
  I already have the database and the listview. But I can't find out how to delete a row.
  Can someone please help me? 
  Here's my DatabaseHelper:</p>
  
  <pre><code>package com.persoonlijk.rooster.test2;
  
  import android.content.Context;
  import android.database.Cursor;
  import android.database.sqlite.SQLiteDatabase;
  import android.database.sqlite.SQLiteOpenHelper;
  import android.database.sqlite.SQLiteStatement;
  import java.util.ArrayList;
  import java.util.List;
  
  public class DataManipulator
  {
  public static final String KEY_ROWID = "id";
  public static final String KEY_DAG = "dag";
  public static final String KEY_UUR = "uur";
  public static final String KEY_VAK = "vak";
  public static final String KEY_LOKAAL = "lokaal";
  
      private static final String DATABASE_NAME = "mydatabase.db";
      static final String TABLE_NAME = "newtable";
      private static final int DATABASE_VERSION = 11;
  
      private static Context context;
      static SQLiteDatabase db;
  
      private SQLiteStatement insertStmt;
      private static final String INSERT = "insert into " + TABLE_NAME + "           (dag,uur,vak,lokaal) values (?,?,?,?)";
     public DataManipulator(Context context) {
     DataManipulator.context = context;
     OpenHelper openHelper = new OpenHelper(DataManipulator.context);
     DataManipulator.db = openHelper.getWritableDatabase();
     this.insertStmt = DataManipulator.db.compileStatement(INSERT);
     }
  
    public long insert(String dag,String uur,String vak,String lokaal) {
    this.insertStmt.bindString(1, dag);
    this.insertStmt.bindString(2, uur);
    this.insertStmt.bindString(3, vak);
    this.insertStmt.bindString(4, lokaal);
    return this.insertStmt.executeInsert();
    }
  
    public void deleteAll() {
    db.delete(TABLE_NAME, null, null);
    }
  
    public List&lt;String[]&gt; selectAll()
    {
    List&lt;String[]&gt; list = new ArrayList&lt;String[]&gt;();
    Cursor cursor = db.query(TABLE_NAME, new String[] { "id","dag","uur","vak","lokaal"  }, null, null, null, null, "dag asc");
    int x=0;
    if (cursor.moveToFirst()) {
     do {
      String[] b1=new String[]    {cursor.getString(0),cursor.getString(1),cursor.getString(2),
    cursor.getString(3),cursor.getString(4)};
      list.add(b1);
      x=x+1;
     } while (cursor.moveToNext());
     }
    if (cursor != null &amp;&amp; !cursor.isClosed()) {
     cursor.close();
     }
    cursor.close();
     return list;
    }
  
    public void delete(int rowId) {
    db.delete(TABLE_NAME, null, null);
    }
  
    private static class OpenHelper extends SQLiteOpenHelper {
    OpenHelper(Context context) {
       super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }
  
  
   @Override
   public void onCreate(SQLiteDatabase db) {
       db.execSQL("CREATE TABLE " + TABLE_NAME + " (id INTEGER PRIMARY KEY, dag TEXT,  uur TEXT, vak TEXT, lokaal TEXT)");
   }
  @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
      {
       db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
       onCreate(db);
   }
   }
  
   }
  </code></pre>
  
  <p>Here is my listview of the data in the database:
       package com.persoonlijk.rooster.test2;</p>
  
  <pre><code> import java.util.ArrayList;
   import java.util.List;
  
   import com.persoonlijk.rooster.test2.SaveData;
  
  import android.app.ListActivity;
  import android.content.Intent;
  import android.os.Bundle;
  import android.view.Menu;
  import android.view.MenuItem;
  import android.view.View;
  import android.widget.ArrayAdapter;
  import android.widget.Button;
  import android.widget.ListView;
  import android.widget.TextView;
  import android.view.View.OnClickListener;
  import android.app.Activity;
  
  public class MainActivity extends ListActivity{    
    TextView selection;
    public int idToModify;
    DataManipulator dm;
  
    List&lt;String[]&gt; list = new ArrayList&lt;String[]&gt;();
    List&lt;String[]&gt; names2 =null ;
    String[] stg1;
  
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState){
  super.onCreate(savedInstanceState);
  setContentView(R.layout.check);
  
      dm = new DataManipulator(this);
      names2 = dm.selectAll();
  
     stg1=new String[names2.size()];
     int x=0;
     String stg;
  
    for (String[] dag : names2) {
      stg = dag[1]+" - "+dag[2]+ " - "+dag[3]+" - "+dag[4];
      stg1[x]=stg;
      x++;
   }
  
  ArrayAdapter&lt;String&gt; adapter = new ArrayAdapter&lt;String&gt;   (this,android.R.layout.simple_list_item_1,stg1);
      this.setListAdapter(adapter);
  selection=(TextView)findViewById(R.id.selection);
  
   }     
  
   public void onListItemClick(ListView parent, View v, int position, long id) {
    selection.setText(stg1[position]);
   }
  
   //menuknoppen
  
   @Override
    public boolean onCreateOptionsMenu(Menu menu) {
      menu.add(Menu.NONE, 0, 0, "Voeg gegevens toe");
      menu.add(Menu.NONE, 1, 1, "Verwijder gegevens");
      return super.onCreateOptionsMenu(menu);
   }
  
   @Override
   public boolean onOptionsItemSelected(MenuItem item) {
       switch (item.getItemId()) {
         case 0:
         startActivity(new Intent(this, SaveData.class));
         return true;
         case 1:
             startActivity(new Intent(this, VerwijderData.class));
             return true;
    }
    return false;
    }
  
   }
  </code></pre>
  
  <p>This is what the code looks like now:
       package com.persoonlijk.rooster.test2;</p>
  
  <pre><code>import java.util.ArrayList;
  import java.util.List;
  
  import android.app.ListActivity;
  import android.content.Intent;
  import android.os.Bundle;
  import android.util.Log;
  import android.view.ContextMenu;
  import android.view.Menu;
  import android.view.MenuItem;
  import android.view.View;
  import android.widget.AdapterView;
  import android.widget.ArrayAdapter;
  import android.widget.ListView;
  import android.widget.TextView;
  
  public class MainActivity extends ListActivity{    
     TextView selection;
     public int idToModify;
     DataManipulator dm;
  
     List&lt;String[]&gt; list = new ArrayList&lt;String[]&gt;();
     List&lt;String[]&gt; names2 =null ;
     String[] stg1;
  
     /** Called when the activity is first created. */
     @Override
     public void onCreate(Bundle savedInstanceState){
     super.onCreate(savedInstanceState);
     setContentView(R.layout.check);
  
      dm = new DataManipulator(this);
      names2 = dm.selectAll();
  
     stg1=new String[names2.size()];
     int x=0;
     String stg;
  
     for (String[] dag : names2) {
         stg = dag[1]+" - "+dag[2]+ " - "+dag[3]+" - "+dag[4];
         stg1[x]=stg;
         x++;
      }
  
    ArrayAdapter&lt;String&gt; adapter = new ArrayAdapter&lt;String&gt;      (this,android.R.layout.simple_list_item_1,stg1);
      this.setListAdapter(adapter);
       selection=(TextView)findViewById(R.id.selection);
     }  
  
    //menuknoppen
     @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(Menu.NONE, 0, 0, "Voeg gegevens toe");
        menu.add(Menu.NONE, 1, 1, "Verwijder gegevens");
        return super.onCreateOptionsMenu(menu);
     }
  
    @Override
     public boolean onOptionsItemSelected(MenuItem item) {
     switch (item.getItemId()) {
         case 0:
             startActivity(new Intent(this, SaveData.class));
         return true;
         case 1:
             startActivity(new Intent(this, VerwijderData.class));
         return true;
  
    }
    return false;
    }
  
    }
  </code></pre>
  
  <p>Right now, in my MainActivity in the menu you can click on 'verwijder' and then you go to the VerwijderData activity. This VerwijderData Activity used to be the MainActivity. In the VerwijderData activity the user deletes the data, and this is the activity where my application won't refresh itself.
  This is the code from the VerwijderData activity:
  package com.persoonlijk.rooster.test2;</p>
  
  <pre><code>import java.util.ArrayList;
  import java.util.List;
  
  import android.app.ListActivity;
  import android.content.Intent;
  import android.os.Bundle;
  import android.util.Log;
  import android.view.ContextMenu;
  import android.view.Menu;
  import android.view.MenuItem;
  import android.view.View;
  import android.widget.AdapterView;
  import android.widget.ArrayAdapter;
  import android.widget.ListView;
  import android.widget.TextView;
  
  public class VerwijderData extends ListActivity{    
    TextView selection;
    public int idToModify;
    DataManipulator dm;
    ArrayAdapter&lt;String&gt; adapter;
  
    List&lt;String[]&gt; list = new ArrayList&lt;String[]&gt;();
    List&lt;String[]&gt; names2 =null ;
    String[] stg1;
  
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.verwijder);
  
      dm = new DataManipulator(this);
      names2 = dm.selectAll();
  
     stg1=new String[names2.size()];
     int x=0;
     String stg;
  
     for (String[] dag : names2) {
         stg = dag[1]+" - "+dag[2]+ " - "+dag[3]+" - "+dag[4];
         stg1[x]=stg;
         x++;
     }
  
    adapter = new ArrayAdapter&lt;String&gt;   (this,android.R.layout.simple_list_item_1,stg1);
    this.setListAdapter(adapter);
    selection=(TextView)findViewById(R.id.selection);
  
   }  
  
  
    public void onListItemClick(ListView parent, View v, int position, long id) {
       String[] delete = names2.get(position);
       String idString = delete[0];
       long idLong = Long.valueOf(idString);
       Log.d("Deleting...", idLong + "");
       dm.delete(idLong);
       names2.remove(position);
  
       stg1=new String[names2.size()];
       int x=0;
       String stg;
  
       for (String[] dag : names2) {
           stg = dag[1]+" - "+dag[2]+ " - "+dag[3]+" - "+dag[4];
           stg1[x]=stg;
           x++;
       }
       adapter.notifyDataSetChanged();
  
    }
  
    }
  </code></pre>
  
  <p>My application has changed a little since I first asked my question, I hope you still understand what I mean.</p>