android - Store new position of RecyclerView items in SQLite after being dragged and dropped -
i have 2 dimensional arraylist
of type string
input adapter class of recyclerview
. data in list taken sqlite database everytime when onresume()
called. have implemented drag , drop feature , onmove()
function swaps list elements successfully. however, need store modified list before onresume()
called rewrite list , fill old positions. guess implement rewriting functionality within onstop()
event. main activity:
recyclerview recyclerview; recyclerviewadapter adapter; recyclerview.layoutmanager layoutmanager; arraylist<arraylist<string>> data; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); toolbar toolbar = (toolbar) findviewbyid(r.id.toolbar); setsupportactionbar(toolbar); floatingactionbutton fab = (floatingactionbutton) findviewbyid(r.id.fab); assert fab != null; fab.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { intent = new intent(mainactivity.this, viewdbactivity.class); startactivity(i); } }); recyclerview = (recyclerview) findviewbyid(r.id.myrecyclerview); layoutmanager = new linearlayoutmanager(this); recyclerview.setlayoutmanager(layoutmanager); recyclerview.itemanimator itemanimator = new defaultitemanimator(); itemtouchhelper item = new itemtouchhelper(new itemtouchhelper.callback() { @override public int getmovementflags(recyclerview recyclerview, recyclerview.viewholder viewholder) { int dragflags = itemtouchhelper.up | itemtouchhelper.down; int swipeflags = itemtouchhelper.right; return makemovementflags(dragflags, swipeflags); } @override public boolean onmove(recyclerview recyclerview, recyclerview.viewholder viewholder, recyclerview.viewholder target) { collections.swap(data, viewholder.getadapterposition(), target.getadapterposition()); adapter.notifyitemmoved(viewholder.getadapterposition(), target.getadapterposition()); return true; } @override public void onmoved(recyclerview recyclerview, recyclerview.viewholder viewholder, int frompos, recyclerview.viewholder target, int topos, int x, int y) { super.onmoved(recyclerview, viewholder, frompos, target, topos, x, y); toast.maketext(mainactivity.this, "moved", toast.length_short).show(); } @override public void onswiped(recyclerview.viewholder viewholder, int direction) { data.remove(viewholder.getadapterposition()); adapter.notifyitemremoved(viewholder.getadapterposition()); } }); item.attachtorecyclerview(recyclerview); recyclerview.setitemanimator(itemanimator); } @override protected void onresume() { super.onresume(); initialize(); if(adapter == null) { log.v(tag, "adapter = null. initialized"); setadapter(); } else { log.v(tag, "notify called"); adapter.updatedata(data); } } public void initialize() { database ourdb = new database(this); ourdb.opendb(); data = ourdb.getdata(); ourdb.closedb(); } public void setadapter() { adapter = new recyclerviewadapter(data, this); recyclerview.setadapter(adapter); }
it important update database long know when store data. how can solve problem? suggestions?
!!!
answer:
final solution have implemented:
@override public void onmoved(recyclerview recyclerview, recyclerview.viewholder viewholder, int frompos, recyclerview.viewholder target, int topos, int x, int y) { super.onmoved(recyclerview, viewholder, frompos, target, topos, x, y); //preparing new position values long moveditem = long.parselong(data.get(frompos).get(0)); // .get(frompos).get(0) returns primary key list long draggeditem = long.parselong(data.get(topos).get(0)); // updating affected rows new orders database db = new database(mainactivity.this); db.open(); db.updateorder(draggeditem, topos); db.updateorder(moveditem, frompos); db.close(); }
and inside of db class:
public void updateorder(long rowid, int newpos) { contentvalues cv = new contentvalues(); cv.put(key_order, newpos); ourdatabase.update(database_table, cv, key_rowid + "=" + rowid, null); }
you need field in each db row storing order. need implements features:
on new row insert (when insert new object in database) need set order field next int. can current max value (with sql function
max
) , +1when user move item in
recyclerview
, in methodonmoved
you must update other rows. can usefrompos
,topos
that. more on belowwhen fill
recyclerview
data need order themorder
field
explanation of 2nd feature implemented: need update rows order between frompos
, topos
:
if user moved item (for example position 4 2), need to:
- get primary key field of current item (using position 4)
- change rows between order 2 , order 4: change 2 -> 3 , 3 -> 4
- change current item order (using primary key of first point)
topos
: in example change current item order 2
if user moved item down (for example position 2 4) need to:
- get primary key field of current item (using position 2)
- change rows between order 2 , order 4: change 4 -> 3 , 3 -> 2
- change current item order (using primary key of first point)
topos
: in example change current item order 4
hope helps little
Comments
Post a Comment