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.
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:
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.
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 MahajanIf 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
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
BlackbeltBlackbelt