有点卡在这里。我可以删除列表视图中的项目,但是也无法从数据库中删除它:
我的listview类:
@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-方法:
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);
}
来自vorgangdbhelper的表行:
public static final String TABLE_VORGAENGE_LIST = "vorgaenge_list";
public static final String COLUMN_ID = "_id";
我的deleteRow给了我一个d`oh。那么我做错了什么?
编辑: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-方法:
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 });
}
答案 0 :(得分:2)
请尝试在dataSource.close()
方法中移动行onDestroy()
。问题是您在onCreate()
。
并将删除方法更新为:
database.delete(VorgangDbHelper.TABLE_VORGAENGE_LIST,VorgangDbHelper.COLUMN_ID + " = ?", new String[] { s_id });