Dbhelper Context

Previous tutorial gives an introduction to SQLite Database in Android. This tutorial explains all CRUD (Create, Retrieve, Update, Delete) functions with example. Example background. Let’s start working with SQLite Database.

  1. Dbhelper Context Analysis
  2. Dbhelper Context Of Life
Active4 years, 1 month ago

I have sqlite data represented on a ListView by a CustomListAdapter.On clicking a row an alert dialogue pops up that prompts the user to delete the single row from sqlite in my activity:

On my DBHelper.java:

However the above code doesn't delete anything.I guess its the function i havent done correctly.Any suggestions?

Full DBHelper.java

EDIT

Logcat Error:

Steve Kamau
Steve KamauSteve Kamau
1,27910 gold badges29 silver badges57 bronze badges

3 Answers

You can use SQL DELETE statement to choose which entry you want to delete.When using a string value as the selector, make sure you enclose the value in '.

Then whenever you want to remove an entry from the database use the following:

UPDATEAssuming that your ListView dataset is an ArrayList containing ContactListItems, you can set an OnItemClickListener for your ListView and use it to get your selected title, and then remove it.

Max TaylerMax Tayler
Dbhelper

I guess the problem in your code is at below line:

Dbhelper Context Analysis

There should be the title as parameter. You are passing nothing. There has to be title that you want to remove from the database. something like below:

Let me know if this not resolved your issue.

UPDATE

For deleting record from the database you must need to define some information like from database, which record you want to delete. It is required to at-least to identify the index of that record.E.g.: You have some title in the database like sport, Joy, Adventure, Fun and Action. Now if you want to delete the record of action from the database. So what you need to do is, You need to pass that title name as parameter in the above function. (deleteSingleContact('Action') in your case). So it will find the index where the title have name as 'Action' and it will delete that record from the database.

Hope you got the point.

Let me know still if you have any doubt.

Sincerely,Shreyash

Shreyash MahajanShreyash Mahajan
14.9k30 gold badges101 silver badges179 bronze badges

If I were in you, I would use a subclass of CursorAdapter, with a CursorLoader, this way, every changes you bring to the database will be reflected immediately also on the ui (your ListView), without any additional line of code. To do so, you will have to add a column called _id, ('_id INTEGER PRIMARY KEY AUTOINCREMENT', in your create table query) to your database.

Assuming that your ListView's dataset is made of ContactListItems, when onItemLongClick, is invoked you can retrieve the object you long-pressed on with

Dbhelper Context

Now, if you change the signature of deleteDialog, like

in onItemLongClick, you can call

Dbhelper Context Of Life

you will have also to change

with

Dbhelper = new databasehelper(context)BlackbeltBlackbelt
134k24 gold badges242 silver badges260 bronze badges

Not the answer you're looking for? Browse other questions tagged androidsqlite or ask your own question.

Posted on