android– 在SQLite中保存重新排序的RecyclerView

android– 在SQLite中保存重新排序的RecyclerView,第1张

概述所以我试图在我的待办事项列表应用程序中实现拖放,但是我在移动时保存行的顺序时遇到问题–即我移动了项目,退出应用程序并恢复到原始位置.我想到了一个解决方案.我的解决方案在我的表中创建一个列:specified_position.每次移动一行时,onItemMove()方法都会返回一个fromPosition

所以我试图在我的待办事项列表应用程序中实现拖放,但是我在移动时保存行的顺序时遇到问题 – 即我移动了项目,退出应用程序并恢复到原始位置.我想到了一个解决方案.

我的解决方案

在我的表中创建一个列:specifIEd_position.每次移动一行时,onItemmove()方法都会返回一个fromposition和toposition.将行的位置移动到toposition,然后递增/更新那里及以上的所有值.每次读取表格时,它都会检查位置并根据它进行排序.

我的问题:

我的解决方案是否正确有没有更好,更容易的方法而不是这样做?非常感谢!

解决方法:

我的待办事项列表应用程序遇到了同样的问题.在这里,我与您分享我的解决方案.

首先,我在Database Helper上有一个updateSortID()函数

@H_502_18@public voID updateSortID(Integer newID,int rowID){ sqliteDatabase db = this.getWritableDatabase(); String strsql = "UPDATE " + table_name_todos + " SET " + ITEM_SORT_ID + "=" + newID + " WHERE " + ID +" = "+ rowID; db.execsql(strsql);}

然后在活动或适配器上,你应该听你的位置更改项目,你需要有这样的功能.它将更新受此位置更改影响的所有项目的所有排序ID.

@H_502_18@@OverrIDepublic voID drop(int from, int to) { // Todo auto-generated method stub Log.d("drop", String.valueOf(from)); Log.d("drop", String.valueOf(to)); ArrayList<Items> temp = db.getTodos(); int tempstart = from; int tempend = to; if (from < to) { Log.d("up to down", "yes"); db.updateSortID(temp.get(tempend).getSortID(), temp.get(tempstart).getID()); for (int i = from; i <= to - 1; i++) { db.updateSortID(temp.get(i).getSortID(), temp.get(i+1).getID()); } } else if (from > to) { Log.d("up to down", "no"); db.updateSortID(temp.get(tempend).getSortID(), temp.get(tempstart).getID()); for (int i = from; i >= to + 1; i--) { db.updateSortID(temp.get(i).getSortID(), temp.get(i-1).getID()); } } ListChanged(); // this is the method I call `notifyDataSetChanged()` method and other related functions} 总结

以上是内存溢出为你收集整理的android – 在SQLite中保存重新排序的RecyclerView全部内容,希望文章能够帮你解决android – 在SQLite中保存重新排序的RecyclerView所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1119436.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-29
下一篇2022-05-29

发表评论

登录后才能评论

评论列表(0条)

    保存