Question
I am trying to use a button to delete the first entry to my database. I thought I had the code right but it does not seem to be deleting any of the data, when the delete button is pressed android is setup so which this as it onClick
method:-
public void delete(View view)
{
datasource.open();
datasource.deleteTitle(0);
datasource.close();
}
the delete method it is calling is this method here:-
public boolean deleteTitle(long rowId)
{
return db.delete(DATABASE_TABLE, KEY_ROWID +
"=" + rowId, null) > 0;
}
is there a different way I should be doing this?
The button is set up like this :-
<Button
android:id="@+id/delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Delete First"
android:onClick="delete"/>
I am using the built in onClick
to go to the method I want. I am using this in other parts of the application and it works just fine.
Solution:
From the accepted answer, I took the code in that and created a call in the DatabaseHelper class names delete first. The code I used was :-
public void deleteFirst()
{
Cursor cursor = db.query(DATABASE_TABLE, null, null, null, null, null, null);
if(cursor.moveToFirst()) {
long rowId = cursor.getLong(cursor.getColumnIndex(KEY_ROWID));
db.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null);
}
}
and I then changed the code I gave above to:
public void delete(View view)
{
datasource.open();
datasource.deleteFirst();
fillData();
datasource.close();
}
with the fillData()
method refreshing the view.