Patrick260284 Patrick260284 - 6 months ago 53
Android Question

Android delete item listview and database

Been a little stuck here. I can delete the item in the listview but have problems deleting it from the database too:

My listview class:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_anzeigen);

final VorgangDataSource dataSource = new VorgangDataSource(this);

Log.d(TAG,"Die Datenquelle wird geƶffnet!");
dataSource.open();

final List<vorgangsdaten> vorgangsdatenList = dataSource.getAllVorgangsDaten();

final ArrayAdapter<vorgangsdaten> VorgangArrayAdapter = new ArrayAdapter<>(
this,
R.layout.mylistlayout,
vorgangsdatenList
);

final ListView lv = (ListView)findViewById(R.id.listView);
lv.setAdapter(VorgangArrayAdapter);
lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

int pos = position;
long i = id;
vorgangsdatenList.remove(pos);
dataSource.deleteRow(i);
VorgangArrayAdapter.notifyDataSetChanged();

return false;
}
});
dataSource.close();



//ActionBar Costumization
android.support.v7.app.ActionBar ab = getSupportActionBar();
ab.setIcon(R.drawable.search1);
ab.setDisplayShowHomeEnabled(true);
ab.setDisplayUseLogoEnabled(true);
}
}


deleterow-method:

public void deleteRow(long id){
String s_id;
s_id = String.valueOf(id);
database.delete(VorgangDbHelper.TABLE_VORGAENGE_LIST,VorgangDbHelper.COLUMN_ID + "="+s_id,null);
}


table-rows from vorgangdbhelper:

public static final String TABLE_VORGAENGE_LIST = "vorgaenge_list";

public static final String COLUMN_ID = "_id";


My deleteRow gives me a d`oh. So what am I doing wrong?

EDIT: ErrorMessage

05-29 21:43:55.829 7725-7725/com.example/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example PID: 7725
java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase: /data/data/com.example/databases/vorgaenge_list.db
at android.database.sqlite.SQLiteClosable.acquireReference(SQLiteClosable.java:55)
at android.database.sqlite.SQLiteDatabase.delete(SQLiteDatabase.java:1613)
at com.example.VorgangDataSource.deleteRow(VorgangDataSource.java:75)
at com.example.anzeigen$1.onItemLongClick(anzeigen.java:71)
at android.widget.AbsListView.performLongPress(AbsListView.java:3659)
at android.widget.AbsListView$CheckForLongPress.run(AbsListView.java:3601)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5679)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
at dalvik.system.NativeStart.main(Native Method)


SOLUTION:

deleteRow-Method:

public void deleteRow(long id){
String s_id;
s_id = String.valueOf(id);

database.delete(VorgangDbHelper.TABLE_VORGAENGE_LIST,VorgangDbHelper.COLUMN_ID + " = ?", new String[] { s_id });
}

Answer

Please try to move line dataSource.close() inside the onDestroy() method instead. The problem is that you close the database in onCreate().

And also update the delete method to: database.delete(VorgangDbHelper.TABLE_VORGAENGE_LIST,VorgangDbHelper.COLUMN_ID + " = ?", new String[] { s_id });