Android:何时将控件绑定到数据库

发布于 2024-11-04 16:27:41 字数 435 浏览 0 评论 0原文

在活动期间什么时候是将控件(例如 textview)绑定到 SQLite 数据库中的值的最佳时间?我想我记得读过你应该在 onStart() 中执行此操作,但我见过的大多数示例都在 onCreate() 中设置值。

下面是代码示例:

//I think this always goes in onCreate
MyDb db = new MyDB();
db.open();
Textview tvTextView;
tvTextView = findViewById(R.id.tv1);

//I'm not sure whether to put this in onCreate(), onStart(), or onResume()
tvTextView.setText(db.getMyText());

通常 MyDb 和 TextView 是整个类的变量。

When is the best time during an activity to bind my controls (such as textview) to values in my SQLite database? I thought I remember reading that you should do this in onStart() but most of the examples I have seen set the values in onCreate().

Here is an example of the code:

//I think this always goes in onCreate
MyDb db = new MyDB();
db.open();
Textview tvTextView;
tvTextView = findViewById(R.id.tv1);

//I'm not sure whether to put this in onCreate(), onStart(), or onResume()
tvTextView.setText(db.getMyText());

Normally MyDb and TextView would be variables for the whole class.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

妄断弥空 2024-11-11 16:27:42

这取决于您想要的行为。如果您只想设置一次文本,onCreate() 就足够了。如果您希望每次 Activity 返回前台时更新文本,您可以使用 onStart()onRestart() 甚至 onResume()。显然,如果您希望更频繁地更新文本(例如每次数据库更改时),您将需要做一些更详细的事情。

It depends on the behavior you desire. If you only want the text to be set a single time, onCreate() will suffice. If you want the text to be updated each time your activity is brought back to the foreground you can use onStart(), onRestart() or even onResume(). Obviously if you want the text to be updated even more often (e.g. every time the database changes) you'll need to do something more elaborate.

痴梦一场 2024-11-11 16:27:42

我建议在片段支持库中使用laoders。此示例演示如何使用加载器(使用 HC API 中的加载器)从联系人数据库加载游标,并在获得第一个结果及其更新时更新列表适配器:

http://developer.android.com/resources/samples/ApiDemos/src /com/example/android/apis/app/FragmentListCursorLoader.html

使用它来填充您自己的数据基本上是相同的,但是当您返回光标时,您可以将其拉到适配器中,而不是在适配器中设置它您想要的数据并将其设置在您的字段中。

有关如何获取这些支持库版本的文章:

http:// /android-developers.blogspot.com/2011/03/fragments-for-all.html

我强烈推荐将此作为从游标和其他来源加载数据的首选现代方法。它会为您处理很多事情,以确保您以最佳方式做事:它异步执行查询,因此您的 UI 不会阻塞等待它,它监视数据的更改,并为您提供一种干净的方法当发生更改时从新游标进行更新,它负责与活动生命周期集成,当活动被销毁以及由于配置更改而创建新实例等时,它会在活动实例之间传播以前的数据。

I suggest using laoders in the fragment support library. This example shows how to use loaders (using the ones in the HC API) to load a cursor from the contacts DB and update a list adapter when you get the first result and as it updates:

http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/FragmentListCursorLoader.html

Using this to fill in your own data is basically the same, but instead of setting it in an adapter when you get the cursor back you can just pull the data you want out and set it in your fields.

Article on how to get the support library version of these:

http://android-developers.blogspot.com/2011/03/fragments-for-all.html

I strongly recommend this as the preferred, modern way to load data from cursors and other sources. It takes care of a lot of things for you to make sure you are doing things the best way: it does the query asynchronously so your UI doesn't block waiting for it, it monitors the data for changes and gives you a clean way to update from a new cursor when there is a change, it takes care of integrating with the activity lifecycle, it propagates previous data across activity instances when an activity is destroyed and new instance created due to a config change, etc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文