如何绕过使用Snackbar撤消SQLite数据撤消操作

时间:2016-05-26 09:54:19

标签: android sqlite android-recyclerview undo snackbar

所以,我正在开发一个项目,我想从RecyclerView中删除一个项目,但我希望用户能够通过在Snackbar中按“UNDO”来恢复该项目。 RecyclerView项目正在从SQLite数据库中提取数据,当用户删除项目时,它将从RecyclerView和数据库中我的表格的相应行中删除它。我的问题是我似乎无法弄清楚如何恢复已删除的数据(SQLite数据)。

我试图在我的DataBaseHelper类中创建一个方法,该方法将位置作为参数,以便我的光标知道要访问哪一行然后我使用游标将该行的所有值分配给AlarmData(我的setter和getter类) )对象并将其返回到我的Adapter类,这样我就可以尝试在Snackbar的UNDO操作方法中恢复表的行。

以下是代码:

DataBaseHelper.java

// Is public AlarmData even legal? that's my setter and getter class
// and the object type I'm trying to return

   public AlarmData recoverAlarms(int position) {

    AlarmData recoverData = new AlarmData();

    String ALARM_RESTORE_QUERY = "SELECT * FROM " + TABLE_ALARMS + " WHERE " + COLUMN_INDEX +
            " = " + position + ";";

    SQLiteDatabase db = getReadableDatabase();
    Cursor cursor = db.rawQuery(ALARM_RESTORE_QUERY, null);

    try {
        if (cursor.moveToFirst()) {
            do {

                recoverData.set_dispLabel(cursor.getString(cursor.getColumnIndex(COLUMN_LABEL)));
                recoverData.set_dispTime(cursor.getString(cursor.getColumnIndex(COLUMN_TIME)));
                recoverData.set_alarmId(cursor.getInt(cursor.getColumnIndex(COLUMN_INDEX)));

            } while (cursor.moveToNext());
        }
    } catch (Exception e) {
        throw e;
    } finally {
        if (cursor != null && !cursor.isClosed()) {
            cursor.close();
            db.close();
        }

    }

    return recoverData;
}

AlarmAdapter.java

   public void deleteAlarm(final int position, final View view) {

    final AlarmData recoverAlarmData = new DataBaseHelper(context, null, null, 1).recoverAlarms(position); //INITIALIZES VARIABLE TO THE OBJECT THAT'S RETURNED
    final AlarmData recoverItem = alarmData.get(position); //Recovers RecyclerView item (this works)
    final DataBaseHelper dataBaseHelper = new DataBaseHelper(context, null, null, 1);


    alarmData.remove(position);
    notifyItemRemoved(position);
    dataBaseHelper.deleteAlarms(position); //DELETES TABLE'S ROW

    Snackbar snackbar = Snackbar.make(view, "Alarm Removed", Snackbar.LENGTH_LONG)
            .setAction("UNDO", new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    alarmData.add(position, recoverItem);
                    notifyItemInserted(position); //RESTORES RECYCLERVIEW ITEM

                    dataBaseHelper.addAlarm(recoverAlarmData); //TRIES TO RECOVER TABLE

                }
            });

    snackbar.show();


}

为了肯定,我想知道如何在我的数据库中复制一个特定的行,这样我就可以在用户点击Snackbar上的UNDO时重新创建它。并且也许可以向我解释我做错了什么。洛尔..

我希望我在这里没有太多表现出我的无聊。非常感谢您的帮助。谢谢!

1 个答案:

答案 0 :(得分:4)

在问之前,我可能应该多花些时间看一下。我在这里找到了完美的解决方案:How can I be notified when a Snackbar has dismissed itself?

显然Snackbars有你可以覆盖的很棒的回调方法。这是文档:https://developer.android.com/reference/android/support/design/widget/Snackbar.Callback.html

我只是使用if语句来检查解雇Snackbar的原因是什么。我将删除表格中的行,不管是因为解雇是针对动作点击(当用户点击" UNDO")时。

我的代码:

<强> AlarmAdapter

        Snackbar snackbar = Snackbar.make(view, "Alarm Removed", Snackbar.LENGTH_LONG)
            .setAction("UNDO", new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    alarmData.add(position, recoverItem);
                    notifyItemInserted(position);

                }

            }).setCallback(new Snackbar.Callback() {
                @Override
                public void onDismissed(Snackbar snackbar, int dismissType) {
                    super.onDismissed(snackbar, dismissType);

                    if(dismissType == DISMISS_EVENT_TIMEOUT || dismissType == DISMISS_EVENT_SWIPE
                            || dismissType == DISMISS_EVENT_CONSECUTIVE || dismissType == DISMISS_EVENT_MANUAL)
                    dataBaseHelper.deleteAlarms(position);

                }
            });