SQLite删除实际上没有删除

时间:2013-07-22 18:06:29

标签: android database android-sqlite

我有一个SQLite数据库,我将用户选择的数据保存到。这些数据将在列表视图中显示,如果您长时间点击数据,它将删除该项目。这是有效的,因为我看到该项目从列表视图中消失,但是当我重新启动应用程序并且所有listview项目都从数据库中恢复时,所有已删除的内容都将返回。我正在使用这个声明:

public void deleteAlarmEntry(int pos){
        Log.i("Deleting item from pos: ", String.valueOf(pos));
        db.delete(MySQLHelper.TABLE_NAME, MySQLHelper.ID_COL + "='" + pos + "'", null);
    }

我可以看到日志中调用的语句。有没有更好的方法来确保语句正确执行?这里有问题吗?

这是我在longA单击listview项目时在MainActivity中调用的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;
}

2 个答案:

答案 0 :(得分:0)

您只是将该项目中的所有内容传递给您的查询。

OnContextMenuItemSelected中,您需要执行类似下面的操作;您需要更改deleteAlarmEntry的参数以接收字符串而不是int。

@Override
public boolean onContextItemSelected(MenuItem item) {
    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
String selectedID;
// adapter is the Adapter from creating the listview
    selectedID = String.valueOf(adapter.getItemId(info.position));
    deleteAlarmEntry(selectedID);
    }

您还需要修改db.delete以返回一个数字,即每个函数int result = db.delete()http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html

编辑:

您可能希望使用OnItemLongClickListener代替OnLongClickListener

http://developer.android.com/reference/android/widget/AdapterView.OnItemLongClickListener.html

lv.setOnItemLongClickListener(new OnItemLongClickListener() {
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
            String id = String.valueOf(alarmAdapter.getItemId(position);
removeItem(id);
            }
    });

第二次编辑: 尝试使用cursoradapter,我建议使用一个简单的游标适配器: http://developer.android.com/reference/android/widget/SimpleCursorAdapter.html

String[] from = new String[]{"time"}; //enter your time column name here
int[] to = new int[]{R.id.time};
Cursor cursor = db.query(MySQLHelper.TABLE_NAME, cols, null, null, null, null, null);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,R.layout.activity_alarm_item, cursor, from, to);
lv.setAdapter(adapter);

答案 1 :(得分:0)

使用,

public void deleteAlarmEntry(int pos) {
    db.delete(MySQLHelper.TABLE_NAME, MySQLHelper.ID_COL + "=?", new String[]{pos + ""});
}