Save new positions in Room after recyclerView drag & drop










0















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:



  • Using SharedPreference + GSON

  • Reading other SQLite answers here on SO like this one: Store new position of RecyclerView items in SQLite after being dragged and dropped


  • Reading Paul Burke's Medium Post

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.










share|improve this question






















  • 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 9: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 3: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 7: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. stackoverflow.com/questions/54867970/…

    – Spritzig
    Feb 25 at 16:52















0















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:



  • Using SharedPreference + GSON

  • Reading other SQLite answers here on SO like this one: Store new position of RecyclerView items in SQLite after being dragged and dropped


  • Reading Paul Burke's Medium Post

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.










share|improve this question






















  • 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 9: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 3: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 7: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. stackoverflow.com/questions/54867970/…

    – Spritzig
    Feb 25 at 16:52













0












0








0


1






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:



  • Using SharedPreference + GSON

  • Reading other SQLite answers here on SO like this one: Store new position of RecyclerView items in SQLite after being dragged and dropped


  • Reading Paul Burke's Medium Post

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.










share|improve this question














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:



  • Using SharedPreference + GSON

  • Reading other SQLite answers here on SO like this one: Store new position of RecyclerView items in SQLite after being dragged and dropped


  • Reading Paul Burke's Medium Post

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.







android android-recyclerview android-room






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 9:50









LittleNobodyLittleNobody

104




104












  • 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 9: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 3: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 7: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. stackoverflow.com/questions/54867970/…

    – Spritzig
    Feb 25 at 16:52

















  • 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 9: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 3: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 7: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. stackoverflow.com/questions/54867970/…

    – Spritzig
    Feb 25 at 16:52
















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 9:55






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 9: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 3:10






@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 3: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 7:35






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 7: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





@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. stackoverflow.com/questions/54867970/…

– Spritzig
Feb 25 at 16:52





@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. stackoverflow.com/questions/54867970/…

– Spritzig
Feb 25 at 16:52












1 Answer
1






active

oldest

votes


















0














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.






share|improve this answer























  • 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.

    – Spritzig
    Feb 24 at 14:54










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53297253%2fsave-new-positions-in-room-after-recyclerview-drag-drop%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














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.






share|improve this answer























  • 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.

    – Spritzig
    Feb 24 at 14:54















0














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.






share|improve this answer























  • 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.

    – Spritzig
    Feb 24 at 14:54













0












0








0







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.






share|improve this answer













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.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 15 '18 at 14:47









Igor IlicIgor Ilic

1,1891915




1,1891915












  • 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.

    – Spritzig
    Feb 24 at 14:54

















  • 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.

    – Spritzig
    Feb 24 at 14:54
















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.

– Spritzig
Feb 24 at 14:54





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.

– Spritzig
Feb 24 at 14:54



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53297253%2fsave-new-positions-in-room-after-recyclerview-drag-drop%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kleinkühnau

Makov (Slowakei)

Deutsches Schauspielhaus