2

I've implemented a recyclerView with a drag and drop feature in my app. Everything works fine until the app is relaunched --any drag and drop changes were not saved/remembered by the app.

I've tried:

My current code looks like this:

In onCreate

 ItemViewModel itemViewModel = ViewModelProviders.of(this).get(ItemViewModel.class);
    itemViewModel.getAllItems().observe(this, new Observer<List<Item>>() {
        @Override
        public void onChanged(List<Item> items) {

         adapter.setItemList(items);
         itemList = items; //Global variable itemList


        }
    });

The method called when item is dragged/moved

private void swap(int firstPosition, int secondPosition) {
    Collections.swap(itemList, firstPosition, secondPosition);
        for(int i = 0; i < itemList.size(); i++) {
            Item currentItem = itemList.get(i);
            ItemViewModel.update(currentItem );
        }
        adapter.notifyItemMoved(firstPosition, secondPosition);
     }

Any ideas how I can let my app save the reordered recyclerView after drag and drop? Thanks in advance.

LittleNobody
  • 55
  • 1
  • 7
  • Does your entity have an `order` property in it ? If it does, you could access your dao and run an `UPDATE` SQL in the `Collections.swap` method when reordering and than on every load of the list, return the items ordered by that property. – Igor Ilic Nov 14 '18 at 09:55
  • @Igor It doesn't, though I did include an `.update(currentItem)` to update each item --somehow this doesn't work – LittleNobody Nov 15 '18 at 03:10
  • If you wish to preserve the order of your items once you close your app or move to another activity, you will need to add a property to your entity files which will server as an order value for it. Since I don't think there is any other way of keeping that order after the app closes – Igor Ilic Nov 15 '18 at 07:35
  • @Igor Thanks for your suggestion. I was able to make it save according to what you said. If you want, please reply as an answer and I'll accept it :) – LittleNobody Nov 15 '18 at 14:42
  • @LittleNobody Please can you show or contact me because I am looking for the same answer I have added a question at SO. I am chaning the positions with swap but it is not saving to the `SQLite`. Below is my question. https://stackoverflow.com/questions/54867970/changing-position-of-recyclerview-is-not-saving-at-sqlite-db – TheCoderGuy Feb 25 '19 at 16:52

1 Answers1

3

If you wish to preserve the order of your items once you close your app or move to another activity, you will need to add a property to your entity files which will server as an order value for it. Once added, you could access your dao and run an UPDATE SQL in the Collections.swap method when reordering items and than on every load of the list, return the items ordered by that property.

Igor Ilic
  • 1,370
  • 1
  • 11
  • 21
  • I have a question I am able to change the position but when I exit the app or go to another activity they are backed to the actual position. Data are saved with `SQLite`. – TheCoderGuy Feb 24 '19 at 14:54