在recyclerview中拖放后,如何在sqlite中保存新位置

vmdwslir  于 2021-06-29  发布在  Java
关注(0)|答案(0)|浏览(265)

我的应用程序上有recyclerview,我可以在我的recyclerview上拖放。我确实使用了sqlite。拖放后如何在sqlite上保存新位置。你能帮助我吗。
这是我的活动。我把行拖到这里。

ItemTouchHelper.SimpleCallback move = new ItemTouchHelper.SimpleCallback(ItemTouchHelper.UP | ItemTouchHelper.DOWN | ItemTouchHelper.START | ItemTouchHelper.END , 0) {
            @Override
            public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
                int from = viewHolder.getAdapterPosition();
                int to = target.getAdapterPosition();
                Collections.swap(Lists,from,to);
                recyclerView.getAdapter().notifyItemMoved(from,to);
                return false;
                //I change items position it there
            }
            @Override
            public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
            }
        };
        ItemTouchHelper itemTouchHelperx = new ItemTouchHelper(move);
        itemTouchHelper.attachToRecyclerView(recyclerView);
    }

这是mydatabasehelper吗

public static final String DATABASE_NAME4 = "mylistxxx.db";
    public static final String TABLE_NAME4 = "mylist_dataxxx";
    public static final String COL14 = "iDxxx";
    public static final String COL24 = "ITEM1xxx";

    public DatabaseHelper4(Context context) {
        super(context, DATABASE_NAME4, null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase dbxxx) {
        String createTable = "CREATE TABLE " + TABLE_NAME4 + " (iDxxx INTEGER PRIMARY KEY AUTOINCREMENT, " +
                " ITEM1xxx TEXT)";
        dbxxx.execSQL(createTable);
    }

    @Override
    public void onUpgrade(SQLiteDatabase dbxxx, int oldVersion, int newVersion) {
        dbxxx.execSQL("DROP IF TABLE EXISTS " + TABLE_NAME4);
        onCreate(dbxxx);
    }

    public boolean addDataxxx(String textt) {
        SQLiteDatabase dbxxx = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(COL24, textt);

        long result = dbxxx.insert(TABLE_NAME4, null, contentValues);

        if (result == -1) {
            return false;
        } else {
            return true;
        }
    }

    public Cursor getListContents() {
        SQLiteDatabase dbxxx = this.getWritableDatabase();
        Cursor dataxxx = dbxxx.rawQuery("SELECT * FROM " + TABLE_NAME4, null);
        return dataxxx;
    }
    public void deleteItem(int iDxxx) {
        SQLiteDatabase db = getWritableDatabase();
        db.execSQL("DELETE FROM " + TABLE_NAME4 + " WHERE " + COL14 + " = " +
                iDxxx);
    }
    public  void  veriTabanıIndexListele(ArrayList<Integer> xxid){
        SQLiteDatabase db = this.getReadableDatabase();
        String[] sutunlar = {"iDxxx"};
        Cursor cursor = db.query("mylist_dataxxx",sutunlar,null,null,null,null,null);
        int idIx = cursor.getColumnIndex("iDxxx");
        while (cursor.moveToNext()){
            xxid.add(cursor.getInt(idIx));
        }
        cursor.close();
        db.close();
    }
}

你能帮助我吗。你怎么能ı 拖放后在sqlite上保存新位置。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题