这个android数据库代码应该放在哪里?
我正在开发一个 Android 应用程序,并且在确定某些代码应该存放在哪里时遇到问题。
该应用程序基于一些 Commonsware 内容以及记事本教程。它有项目,每个项目都可以打开、编辑属性或“运行”。
现在,有一个 dbadapter 类,它具有“项目”的基本 CRUD 操作。将 open、edit 或 run 方法放在那里似乎没有意义,因为这些方法涉及启动活动,而我无法从 dbadapter 内部执行此操作。
我目前在项目的详细视图中拥有代码。我希望能够从详细视图之外的其他活动的上下文菜单中调用相同的方法。
代码应该放在哪里?我应该上项目课吗?它将如何与 dbadapter 和游标交互?我是否应该制作某种看不见的片段——可能不应该,但我有点抓住救命稻草了?
I'm working on an android application, and I'm having an issue figuring out where some code should live.
The application is based on some of the Commonsware stuff, along with the Notepad tutorial. It has projects, and each project can be opened, have properties edited, or "ran".
Right now, there's a dbadapter class that has the basic CRUD operations for a "project". It doesn't seem to make sense to put the open, edit, or run methods in there, because those methods involve starting activities, and I can't do that from inside the dbadapter.
I currently have the code inside the detail view for the project. I want to be able to call those same methods from context menus in other activites than the detail view.
Where should the code live? Should I have a Project class? How would that interface with the dbadapter and cursors? Should I make some sort of invisible Fragment--probably not, but I'm grasping at straws a little bit?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该将数据库代码与活动分开。在数据库适配器中进行 CRUD 操作是一个很好的做法。您还可以在该数据库之上编写一个提供程序,该提供程序可以在顶层使用这些 CRUD 操作来维护您的活动和数据库之间的社区。
You should seperate your database code from the activities.. Having CRUD operations in DB adapter is a good practice. You can also write a provider on top of that database which can use those CRUD operations at the top level to maintain the community between your activities and the database.