android - recycleView item on drop is not getting sorted properly with sqlite

前端 未结 2 832
遥遥无期
遥遥无期 2021-01-21 18:48

I have implemented a recycle view On drag & drop the sorting item in the recycle view. But sorting is not accurate, sorting not working properly. I tried write below code

相关标签:
2条回答
  • 2021-01-21 19:16

    I guess the problem is that the order value of ExerciseDetails has never been updated in addition to some confusion in changing orders. However, I have changed your code a bit to fix these issues.

    As both updateOrder and updateSortID exactly execute the same query, so I use only updateOrder and it seems that orderNewValue should be long (not String).

    public void updateOrder(Long orderNewValue, Long rowId) {
        String query = "Update " + DBHelper.TABLE_EXERCISE_DETAILS + " Set " + DBHelper.COLUMN_ORDER_ID + " = " + orderNewValue +  " Where " + DBHelper.COLUMN_EX_DETAILS_ID + " = " + rowId;
        mDatabase.execSQL(query);
    }
    

    and onDrop becomes like following:

    @Override
    void onDrop(int from, int to) {
        // retain order of `to` item before changing orders:
        long orderTo = items.get(to).getOrder();
    
        // swap order values for `to` item and other middle items (except `from` item):
        if (from < to) {
            for (int i = to; i > from; i--) {
                ExerciseDetails item = items.get(i);
                long newOrder = items.get(i - 1).getOrder();
                item.setOrder(newOrder);
                exercisesDAO.updateOrder(newOrder, item.getExDetailsId());
            }
        } else {
            for (int i = to; i < from; i++) {
                ExerciseDetails item = items.get(i);
                long newOrder = items.get(i + 1).getOrder();
                item.setOrder(newOrder);
                exercisesDAO.updateOrder(newOrder, item.getExDetailsId());
            }
        }
    
        // set order for `from` item after swaping orders:
        ExerciseDetails itemFrom = items.get(from);
        itemFrom.setOrder(orderTo);
        exercisesDAO.updateOrder(orderTo, itemFrom.getExDetailsId());
    }
    

    After all, note that you should never do an I/O job in the main thread. So, consider to run updateOrder in another thread somehow based on your app structure.

    0 讨论(0)
  • 2021-01-21 19:35
    
    @Overid
    private void onDrop(int from, int to) {   // this listener only calls when item Droped after drag
        ExerciseDetails moveItem = items.remove(from);
        items.add(moveItem, to)
        int minPosition = from > to ? to : from;
        int maxPosition = from > to ? from : to;
        // now the item index is the sort index of ExerciseDetails
        for (int i = minPosition; i <= maxPosition; i++) {
            ExerciseDetails item = items.get(i);
            item.setOrder(i);
            exercisesDAO.updateOrder(i, item.getExDetailsId());
        }
        // notify recyclerview to refresh the position of item dragged
        adapter.notifyItemMoved(from, to)
    }
    
    0 讨论(0)
提交回复
热议问题