When I try to delete an item in list view ,it removes it from list view but when I add a new item or run the application again the items is still there.I can't delete it from database.
I am using this code:
...
SqlHandler sqlHandler;
ListView myListView;
myAdapter adapter;
ArrayList<myItems> items;
...
myListView = (ListView) findViewById(R.id.myListView);
sqlHandler = new SqlHandler(this);
items = getItemsFromDatabase();
adapter = new myAdapter(this, items);
myListView.setAdapter(adapter);
myListView.setOnItemLongClickListener(this);
...
public boolean onItemLongClick(AdapterView<?> adapterView, View view,
int position, long id) {
final myItems selectedItem = items.get(position);
if (selectedItem != null) {
AlertDialog.Builder alt_bld = new AlertDialog.Builder(this);
alt_bld.setMessage("Do you want to delete this item?")
.setCancelable(false)
.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
sqlHandler=new SqlHandler(getApplicationContext());
sqlHandler.deleteRecord(id);
items.remove(selectedItem);
adapter.notifyDataSetChanged();
For the database I use :
public class SqlHandler {
public static final String DATABASE_NAME = "MY_DATABASE";
public static final String DATABASE_TABLE = "MEM";
public static final String KEY_ROWID = "_id";
public static final int DATABASE_VERSION = 1;
Context context;
SQLiteDatabase sqlDatabase;
SqlDbHelper dbHelper;
public SqlHandler(Context context) {
dbHelper = new SqlDbHelper(context, DATABASE_NAME, null,
DATABASE_VERSION);
sqlDatabase = dbHelper.getWritableDatabase();
}
public void executeQuery(String query) {
try {
if (sqlDatabase.isOpen()) {
sqlDatabase.close();
}
sqlDatabase = dbHelper.getWritableDatabase();
sqlDatabase.execSQL(query);
} catch (Exception e) {
System.out.println("DATABASE ERROR " + e);
}
}
public Cursor selectQuery(String query) {
Cursor c1 = null;
try {
if (sqlDatabase.isOpen()) {
sqlDatabase.close();
}
sqlDatabase = dbHelper.getWritableDatabase();
c1 = sqlDatabase.rawQuery(query, null);
} catch (Exception e) {
System.out.println("DATABASE ERROR " + e);
}
return c1;
}
public SqlHandler open() throws SQLException
{
sqlDatabase = dbHelper.getWritableDatabase();
return this;
}
public void Close(){
dbHelper.close();
}
public void deleteRecord(long rowId){
try {
sqlDatabase.delete(DATABASE_TABLE,KEY_ROWID + "="+rowId,null);
}
catch (Exception e)
{
Log.e("DB ERROR", e.toString());
e.printStackTrace();
}
}
and
public class SqlDbHelper extends SQLiteOpenHelper {
public static final String DATABASE_TABLE = "MEM";
public static final String TAG="DbHandler";
public static final String KEY_ROWID = "_id";
...
private static final String SCRIPT_CREATE_DATABASE = "create table "
+ DATABASE_TABLE + " (" + KEY_ROWID
+ " integer primary key autoincrement, " + ....);";
public SqlDbHelper(Context context, String name, CursorFactory factory,
int version) {
super(context, name, factory, version);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(SCRIPT_CREATE_DATABASE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
onCreate(db);
}
----------UPDATE---------------------
It should be sth like:
myItems theItems =(myItems)adapterView.getItemAtPosition(button_id);
String _id = theItems.getID();
String delQuery = "DELETE FROM MEM WHERE _id='"+_id+"' ";
sqlHandler.executeQuery(delQuery);
but I have a problem with the (myItems)adapterView.. I am not sure how to handle this.