Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9066411
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T16:43:35+00:00 2026-06-16T16:43:35+00:00

I made an Android application with a SQLite database. I want the user to

  • 0

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:

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<String[]> selectAll()
  {
  List<String[]> list = new ArrayList<String[]>();
  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 && !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);
 }
 }

 }

Here is my listview of the data in the database:
package com.persoonlijk.rooster.test2;

 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<String[]> list = new ArrayList<String[]>();
  List<String[]> 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<String> adapter = new ArrayAdapter<String>   (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;
  }

 }

This is what the code looks like now:
package com.persoonlijk.rooster.test2;

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<String[]> list = new ArrayList<String[]>();
   List<String[]> 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<String> adapter = new ArrayAdapter<String>      (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;
  }

  }

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;

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<String> adapter;

  List<String[]> list = new ArrayList<String[]>();
  List<String[]> 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<String>   (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();

  }

  }

My application has changed a little since I first asked my question, I hope you still understand what I mean.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T16:43:37+00:00Added an answer on June 16, 2026 at 4:43 pm

    Firstly we need to re-work your database delete method. it won’t do anything the way it is.

    public boolean delete(long rowId) {
    /*      this is what your database delete method should look like
            this method deletes by id, the first column in your database*/
            return db.delete(TABLE_NAME, KEY_ROWID + "=" + rowId, null) > 0;
        }
    

    Now the rest is in the Activity’s onListItemClick method

        public void onListItemClick(ListView parent, View v, int position, long id) {
    /*      it looks like you're using an ArrayAdapter so you're gonna need to delete
            both from the listview and from the database. 
            first we refer specifically to the corresponding item in the list by position
            which is provided by the listItemClick parameter*/
            String[] delete = names2.get(position);
    /*      we do this because we want the id to delete by in the database by id
            based on your dm.selectAll() method, it should be at the first index of the array*/
            String idString = delete[0];
    //      you've made it into a string, but we need it back to being a long value
            long idLong = Long.valueOf(idString);
            Log.d("Deleting...", idLong + "");
    /*      i put in the log statement for debugging purposes, it will put the id value on Logcat
            so if something goes wrong, we can at least rule out the step since we see the value being
            removed, but!
            finally we can delete it from our database because we have the parameter we need*/
            dm.delete(idLong);
    /*      There it's done, but surely you want it off the listview too so no one is confused. 
            unfortunately you've made stg1 an array so we can't just remove the index from it*/
            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++;
            }
    /*      can you see what happened? the `List` collection is able have its components dynamically
            added and removed, so i removed from that, then i remade the stg1 collection using your method.*/
            adapter.notifyDataSetChanged();
    //      ^ that command refreshes the listview to see changes.
        }
    

    All things considered, you’ve made this task much more tedious than it needs to be with your current setup. Firstly you’ve made plenty of intermediary collections before inputting to your adapter, then you’ve put an array as the datasource, which can’t be dynamically reszied.

    More satisfactory would be to have a setup utilizing a CursorAdapter. Then you could make use of the id parameter provided on the ListItemClick method as it would be the exact id that we wanted. Moreover, you’d only need to delete from the one database source, and simply refresh the adapter afterwards.


    Make your adapter a field like some of those other variables in order to refrence it throughout the entire class:

    public class MainActivity extends ListActivity{    
        TextView selection;
        public int idToModify;
        DataManipulator dm;
        ......
        ArrayAdapter<String> adapter; <--------- add that
    
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState){
            super.onCreate(savedInstanceState);
            setContentView(R.layout.check);
            .........
            adapter = new ArrayAdapter<String>   (this,android.R.layout.simple_list_item_1,stg1); <------ edit that
            this.setListAdapter(adapter);
        }     
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have made an application in android that lets the user compress and decompress
I have made an application in android and used Sqlite database for storing data.
I have made a ListView in my android application. But the problem is that
I have a pre-baked sqlite database that I want to use in my Android
I made an android application inside that i have put an apk file in
I made an application that copies a database that has been premade and then
I'm implementing an Android Application now. I made a database in the application, and
I made ​​an Android application that uses that uses the SearchManager. The problem is
Last year I made an Android application that scrapped the informations on my train
I made a simple android application that moves from one activity to another activity

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.