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 6625251
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:45:04+00:00 2026-05-25T21:45:04+00:00

i want to delete a row in the database table i using a listview

  • 0

i want to delete a row in the database table
i using a listview and after longclick the listitem you will get alert dialog to show edit and delete method
when i click the edit method the emulator goes fine, but when i click the delete method, there comes the force close notification,.im new to android programming,
can you explain what is the problem with my code
this is my code
for database helper:

import android.content.Context;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase;

class PuraHelper extends SQLiteOpenHelper {

private SQLiteDatabase db;
public final String TABLE_NAME = "pura";
public final String TABLE_ROW_ID = "_id";
public final String NAMA_PURA = "nama_pura";
public final String KABUPATEN = "kabupaten";
public final String JALAN = "jalan";
public final String LONGITUDE = "longitude";
public final String LATITUDE = "latitude";
public final String KETERANGAN = "keterangan";

private static final String DATABASE_NAME="PPB.db";
private static final int SCHEMA_VERSION=1;

public PuraHelper(Context context) {
    super(context, DATABASE_NAME, null, SCHEMA_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    String CreateTablePura="create table"+TABLE_ROW_ID+"INTEGER PRIMARY KEY AUTOINCREMENT,"+ NAMA_PURA + "TEXT," + KABUPATEN + "TEXT,"+ JALAN + "TEXT," +LONGITUDE + "TEXT," + LATITUDE + "TEXT," + KETERANGAN + "TEXT";
    db.execSQL(CreateTablePura);

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // no-op, since will not be called until 2nd schema
    // version exists
}

public Cursor getAll() {
    String SelectAll = "SELECT _id, nama_pura, kabupaten, jalan, longitude, latitude, keterangan FROM pura ORDER BY nama_pura";
    return(getReadableDatabase()
                    .rawQuery(SelectAll,
                            null));
}

public Cursor getById(String id) {
    String SelectById = "SELECT _id, nama_pura, kabupaten, jalan, longitude, latitude, keterangan FROM pura WHERE _ID=?";
    String[] args={id};
    return(getReadableDatabase()
                    .rawQuery(SelectById, args));
}

public void insert(String nama_pura, String kabupaten, String jalan, String longitude, String latitude, String keterangan) {
    ContentValues cv=new ContentValues();

    cv.put("nama_pura", nama_pura);
    cv.put("kabupaten", kabupaten);
    cv.put("jalan", jalan);
    cv.put("longitude", longitude);
    cv.put("latitude", latitude);
    cv.put("keterangan", keterangan);
    getWritableDatabase().insert("pura", "nama", cv);
}

public void update(String id, String nama_pura, String kabupaten, String jalan, String longitude, String latitude, String keterangan) {
    ContentValues cv=new ContentValues();
    String[] args={id};

    cv.put("nama_pura", nama_pura);
    cv.put("kabupaten", kabupaten);
    cv.put("jalan", jalan);
    cv.put("longitude", longitude);
    cv.put("latitude", latitude);
    cv.put("keterangan", keterangan);
    getWritableDatabase().update("pura", cv, "_ID=?", args);
}

public void deleteRow(long rowId)
{
    // ask the database manager to delete the row of given id
    //db.delete(TABLE_NAME, TABLE_ROW_ID + "=" + rowId, null);

    db.delete(TABLE_NAME, TABLE_ROW_ID + "="+rowId,null);

}


public String getId(Cursor c) {
    return(c.getString(0));
}
public String getNama(Cursor c) {
    return(c.getString(1));
}
public String getKab(Cursor c) {
    return(c.getString(2));
}
public String getJalan(Cursor c) {
    return(c.getString(3));
}
public String getLong(Cursor c) {
    return(c.getString(4));
}
public String getLat(Cursor c) {
    return(c.getString(5));
}
public String getKet(Cursor c) {
    return(c.getString(6));
}

for my java class:

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CursorAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemLongClickListener;

public class db_list extends ListActivity {

protected static final String ID_EXTRA = "com.jigler.ppb._ID";
PuraHelper helper=null;
Cursor model=null;
PuraAdapter adapter=null;

EditText    etNama, etKabupaten,etJalan, etLong, etLat, etKeterangan;
Button      bSave, bCancel;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tab_list);

    helper=new PuraHelper(this);

    etNama = (EditText)findViewById(R.id.etNama);
    etKabupaten = (EditText)findViewById(R.id.etKabupaten);
    etJalan = (EditText)findViewById(R.id.etJalan);
    etLong = (EditText)findViewById(R.id.etLong);
    etLat = (EditText)findViewById(R.id.etLat);
    etKeterangan = (EditText)findViewById(R.id.etKeterangan);

    model=helper.getAll();
    startManagingCursor(model);
    adapter=new PuraAdapter(model);
    setListAdapter(adapter);

     ListView lv = getListView();
     lv.setLongClickable(true);
     lv.setOnItemLongClickListener(new OnItemLongClickListener() {

        public boolean onItemLongClick(AdapterView<?> list, View view,
                int position, long id) {
            // TODO Auto-generated method stub
            alert_editlist(id);
            return true;
        }           
     });
}

@Override
public void onDestroy() {
    super.onDestroy();
    helper.close();
}

@Override
public void onListItemClick(ListView list, View view, int position, long id) {
    Intent i=new Intent(db_list.this, db_new_man.class);

    i.putExtra(ID_EXTRA, String.valueOf(id));
    startActivity(i);
}

private void alert_editlist(final long id) {
    // TODO Auto-generated method stub
    final CharSequence[] items = {"Edit", "Delete"};

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("What do you want to do?");
    builder.setIcon(R.drawable.ic_setting_36);
    builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int item) {

            if (items[item]=="Edit") {
                Intent i=new Intent(db_list.this, db_new_man.class);
                i.putExtra(ID_EXTRA, String.valueOf(id));
                startActivity(i);
            }
            else if(items[item]=="Delete"){
                                    //this is where my app force close
                helper.deleteRow(id);
                model.requery();
            }
            finish();
        }
    });
    AlertDialog alert = builder.create();
    alert.show();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    new MenuInflater(this).inflate(R.menu.option, menu);
    return(super.onCreateOptionsMenu(menu));
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId()==R.id.add) {
        //startActivity(new Intent(db_list.this, db_new.class));
        alert_db_new();
        return(true);
    }
    return(super.onOptionsItemSelected(item));
}

private void alert_db_new() {
    // TODO Auto-generated method stub
    final CharSequence[] items = {"Auto", "Manual"};

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Use Your Configuration Setting By");
    builder.setIcon(R.drawable.ic_setting_36);
    builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int item) {

            if (items[item]=="Auto") {
                startActivity(new Intent(db_list.this, db_new_auto.class));
            }
            else if(items[item]=="Manual"){
                startActivity(new Intent(db_list.this, db_new_man.class));
            }
            finish();
        }
    });
    AlertDialog alert = builder.create();
    alert.show();
}



/*
private View.OnClickListener onSave=new View.OnClickListener() {
    public void onClick(View v) {
        helper.insert
        (
                etNama.getText().toString(),
                etKabupaten.getText().toString(),
                etJalan.getText().toString(),
                etLong.getText().toString(),
                etLat.getText().toString(),
                etKeterangan.getText().toString()
        );
        model.requery();

    }
};
*/

class PuraAdapter extends CursorAdapter {
    PuraAdapter(Cursor c) {
        super(db_list.this, c);
    }
    @Override
    public void bindView(View row, Context ctxt, Cursor c) {
        // TODO Auto-generated method stub
        PuraHolder holder=(PuraHolder)row.getTag();

        holder.populateFrom(c, helper);
    }

    @Override
    public View newView(Context ctxt, Cursor c, ViewGroup parent) {
        // TODO Auto-generated method stub
        LayoutInflater inflater=getLayoutInflater();
        View row=inflater.inflate(R.layout.row, parent, false);
        PuraHolder holder=new PuraHolder(row);

        row.setTag(holder);

        return(row);
    }
}
static class PuraHolder {
    private TextView nama=null;
    private TextView alamat=null;
    private ImageView icon=null;

    PuraHolder(View row) {
        nama=(TextView)row.findViewById(R.id.nama_pura);
        alamat=(TextView)row.findViewById(R.id.jalan);
        icon=(ImageView)row.findViewById(R.id.icon_pura);
    }

    void populateFrom(Cursor c, PuraHelper helper) {
        nama.setText(helper.getNama(c));
        alamat.setText(helper.getJalan(c));
        icon.setImageResource(R.id.icon_pura);
    }
}

 }

thank you for the attention

  • 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-05-25T21:45:05+00:00Added an answer on May 25, 2026 at 9:45 pm

    I think you didn’t instantiate “db” in your code.

    using

    getWritableDatabase().delete(TABLE_NAME, TABLE_ROW_ID + "="+rowId,null)
    

    instead of

    db.delete(TABLE_NAME, TABLE_ROW_ID + "="+rowId,null)
    

    may help.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to delete several row mysql database together after select with checkbox in
I want to create a new row in my database on a table that
I want to delete a row in the grid, but the grid always post's
I want to delete an DOM element right after fading out. What I did
I have a table in my database which has duplicate records that I want
I want to get the number of NOT NULL records from my SQLite database.
I have couple of rows in database table (lets call it Customer), each row
My database is currently using the InnoDB engine. Now I want to add the
I want to bind a database table to a swing JTable, and make that
I'm try to remove the deleted table row after it's been successfully deleted from

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.