0

ユーザーが選択したデータを保存する 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 で呼び出される私の removeItem メソッドは次のとおりです。

public void removeItem(int position) {
    alarmItemArray.remove(position);
    dataSource.deleteAlarmEntry(position);
    alarmAdapter.notifyDataSetChanged();
}

dataSource.deleteAlarmEntry() は、上記のデータベースの削除を呼び出します。

また、アプリケーションの起動時に、エントリを一時配列リストに取り込み、次のように時間を解析してアダプタ配列リストを取得しています。

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;
}
4

2 に答える 2

0

使用する、

public void deleteAlarmEntry(int pos) {
    db.delete(MySQLHelper.TABLE_NAME, MySQLHelper.ID_COL + "=?", new String[]{pos + ""});
}
于 2013-07-22T18:57:27.733 に答える