我有一个SQLite数据库,我正在保存用户选择的数据。这些数据将在列表视图中显示,如果您长时间点击数据,它将删除该项目。这是工作,因为我看到项目从列表视图中消失,但是当我重新启动应用程序和所有列表视图项从数据库中带回来时,所有被删除的内容都会回来。我使用这样的说法:SQLite删除没有实际删除
public void deleteAlarmEntry(int pos){
Log.i("Deleting item from pos: ", String.valueOf(pos));
db.delete(MySQLHelper.TABLE_NAME, MySQLHelper.ID_COL + "='" + pos + "'", null);
}
我能看到的语句被称为在日志中。有没有更好的方法来确保语句正确执行?这里有什么不对吗?
这是我叫在MainActivity的ListView项的长按RemoveItem方法:
public void removeItem(int position) {
alarmItemArray.remove(position);
dataSource.deleteAlarmEntry(position);
alarmAdapter.notifyDataSetChanged();
}
的dataSource.deleteAlarmEntry()调用上面数据库中删除。
此外,在应用程序启动时,我把条目进入一个临时的ArrayList,然后解析的时间来获取适配器的ArrayList像这样:
dataSource = new WeatherDataSource(this);
dataSource.open();
ArrayList<AlarmEntry> alarmEntries = (ArrayList<AlarmEntry>) dataSource.getAllWeatherEntries();
alarmItemArray = getTimeFromEntries(alarmEntries);
alarmAdapter = new ArrayAdapter<String>(this,
R.layout.activity_alarm_item, R.id.time, alarmItemArray);
lv = (MyListView) findViewById(R.id.listview);
lv.setAdapter(alarmAdapter);
这里是数据库的getAllWeatherEntries:
public List<AlarmEntry> getAllWeatherEntries(){
List<AlarmEntry> weatherEntry = new ArrayList<AlarmEntry>();
Cursor cursor = db.query(MySQLHelper.TABLE_NAME, cols, null, null, null, null, null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
AlarmEntry m = cursorToEntry(cursor);
weatherEntry.add(m);
Log.i("Get all weather entries", m.getTime());
cursor.moveToNext();
}
cursor.close();
return weatherEntry;
}
在这里添加您的全logcat的,请。 – Sajmon
07-22 12:55:57.683:I /从pos删除项目:(29438):8 – worm
并且您是否确定该项目也从数据库中删除,而不仅仅是从列表中删除? – Sajmon