How can I delete a specific row from an sql table?
I have a listview populated from an sql table. E.g. there are 5 records in the table and so are in the listview. When I click on the 3rd item in the listview I want to delete the 3rd record from the sql table. The row id is of course out of the question, since it differs from the row number. What is the sql syntax for this?
This is my populate query:
public Cursor getAllTitles() {
String[] columns = new String[]{KEY_ROWID, KEY_NAME, KEY_HOTNESS};
return ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);}
and my delete query:
public void deleteEntry(int lRow1) {
ourDatabase.delete(DATABASE_TABLE, KEY_ROWID + "=" + lRow1, null);}
This is how I put the cursor into an arraylist:
Cursor c = info.getAllTitles();
if (c.moveToFirst())
{
do{
todoItems.add(c.getString(0) + " " + c.getString(1) + " " + c.getString(2));
}while (c.moveToNext());
}
The textView1 is the id of the TextView in my listview layout. And this is my setOnClickListener:
holder.textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TextView tv = (TextView) v ;
String text_of_clicked_textview = tv.getText().toString();
selected_row_num = (Integer)v.getTag();
Log.i("TAG", "" + text_of_clicked_textview + selected_row_num);
}
});
When I click on the 3rd item of the ListView, I get item2 in the logcat through Log.i.
In the getAllTitles() query I get the KEY_ROWID as well (for the time being) but since this cannot be in the text of textview, I needed the row number of the clicked item.
All I need is to change the deleteEntry query to remove the 3rd item.
When you’re filling your tableView, you can store the ID of this field in the View (i.e.
view.setTag(new Integer(cursor.getInt(FIELD_ID)));)You can access the ID with
int id = ((Integer)view.getTag()).intValue();