Swipe中的一些问题可以删除recyclelerview

时间:2017-02-03 11:09:26

标签: android android-recyclerview recycler-adapter swipe-gesture android-database

以下是我向左或向右滑动时用于删除项目的代码,现在问题是我在快餐栏中放置了UNDO按钮。 当我轻扫我从数据库中删除项目时,如何点击UNDO时,因为当按下UNDO时,翻转的项目应该回来。

  if (direction == ItemTouchHelper.LEFT){
                //adapterr.removeItem(position);
                Snackbar snackbar = Snackbar
                        .make(recyclerView, "1 Song deleted", Snackbar.LENGTH_LONG)
                        .setAction("UNDO", new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                              /*  Snackbar snackbar1 = Snackbar.make(recyclerView, "", Snackbar.LENGTH_SHORT);
                                snackbar1.show();*/

                            }
                        });

                snackbar.show();
                openDatabase();
                String whereClause = QueueDatabase.KEY_ID + " = " + queuee.get(position).getId();
                String[] whereArgs = null;
                db.delete(QueueDatabase.TABLE_QUEUE,whereClause,null);
                queuee.remove(position);
                adapterr.notifyItemRemoved(position);
                Toast.makeText(QueueActivity.this, "removed", Toast.LENGTH_SHORT).show();
            }
            else {
               Snackbar snackbar = Snackbar
                        .make(recyclerView, "1 Song deleted", Snackbar.LENGTH_LONG)
                        .setAction("UNDO", new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                              /*  Snackbar snackbar1 = Snackbar.make(recyclerView, "", Snackbar.LENGTH_SHORT);
                                snackbar1.show();*/

                            }
                        });

                snackbar.show();
                openDatabase();
                String whereClause = QueueDatabase.KEY_ID + " = " + queuee.get(position).getId();
                String[] whereArgs = null;
                db.delete(QueueDatabase.TABLE_QUEUE,whereClause,null);
                queuee.remove(position);
                adapterr.notifyItemRemoved(position);
                Toast.makeText(QueueActivity.this, "removed", Toast.LENGTH_SHORT).show();
            }

queuee是一个数组列表,用于从数据库中收集所有数据,然后填充回收器视图。 请帮忙,谢谢

1 个答案:

答案 0 :(得分:0)

复制已删除的项目&它在其他变量中的位置,如

Time  |  Max (in/h) |   Total Rainfall(in)  | Events 
------------------------------------------------------------------------------
2016-08-15 3:10  |  3.67500 |     2.294     | Event 1
----------------------------------------------------------------------------
2016-08-15 3:15  |  3.67500 |     2.294     | Event 1     
-----------------------------------------------------------------------------
2016-08-15 3:20  |  3.67500 |     2.294     | Event 1       
------------------------------------------------------------------------------
||
------------------------------------------------------------------------------
2016-08-15 19:08 |  3.67500 |     2.294     | Event 1   (Upto End datetime)
-----------------------------------------------------------------------------
||
------------------------------------------------------------------------------
2016-08-01 17:03 |  0.1371  |     0.1023    | Event 47  (Similarly for all events)
------------------------------------------------------------------------------

点击撤消

时将项目插入相同位置
   int deletedPosition = position;
   YourItemType DeletedItem = YourList.get(position);

我把这段代码放在适配器中。