Android SQLite 延迟
我有这个代码,这是我找到的一个例子。在此代码中,我插入 2 行,然后读取它们。我想做的是读取第一行并等待 5 秒,然后读取第二行并等待 5 秒。如何做到这一点?
这是代码:
package net.learn2develop.Database;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.Toast;
public class DatabaseActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
DBAdapter db = new DBAdapter(this);
//---add 2 titles---
db.open();
long id;
id = db.insertTitle(
"0470285818",
"java ++ :)",
"someone");
id = db.insertTitle(
"047017661X",
"Professional Programming",
"someone2");
db.close();
//---get all titles---
db.open();
Cursor c = db.getAllTitles();
if (c.moveToFirst())
{
do {
DisplayTitle(c);
} while (c.moveToNext());
}
db.close();
}
public void DisplayTitle(Cursor c)
{
Toast.makeText(this,
"id: " + c.getString(0) + "\n" +
"ISBN: " + c.getString(1) + "\n" +
"TITLE: " + c.getString(2) + "\n" +
"PUBLISHER: " + c.getString(3),
Toast.LENGTH_LONG).show();
}
}
I have this code that is an example I found. In this code, I insert 2 rows and then read them. What I want to do is read the first row and wait 5 seconds then read the second row and wait for 5 seconds. How that can be done?
this is the code:
package net.learn2develop.Database;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.Toast;
public class DatabaseActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
DBAdapter db = new DBAdapter(this);
//---add 2 titles---
db.open();
long id;
id = db.insertTitle(
"0470285818",
"java ++ :)",
"someone");
id = db.insertTitle(
"047017661X",
"Professional Programming",
"someone2");
db.close();
//---get all titles---
db.open();
Cursor c = db.getAllTitles();
if (c.moveToFirst())
{
do {
DisplayTitle(c);
} while (c.moveToNext());
}
db.close();
}
public void DisplayTitle(Cursor c)
{
Toast.makeText(this,
"id: " + c.getString(0) + "\n" +
"ISBN: " + c.getString(1) + "\n" +
"TITLE: " + c.getString(2) + "\n" +
"PUBLISHER: " + c.getString(3),
Toast.LENGTH_LONG).show();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不应该将任何长时间运行的函数放在主线程中。 onCreate 必须始终快速执行然后返回,否则它们可能会被 Android 操作系统作为 ANR(应用程序无响应)杀死。
考虑使用 TimerTask,让您的查询获取第 n 条记录,每次递增 n。请注意,TimerTask 将在线程中运行您的方法,并且您只能从主线程进行 UI 调用。使用 Activty.runOnUiThread() 来解决这个问题。
You should not put any long-running function in the main thread. onCreate must always execute quickly and then return, otherwise they may be killed by android OS as an ANR (Application Not Responding).
Consider using a TimerTask, have your query get the nth record, incrementing n each time. Be aware that TimerTask will run your method in a thread and you can only make UI calls from the main thread. Use Activty.runOnUiThread() to work around that.
正确的方法 - 它不会阻塞您的主线程,因此 UI 保持响应:
The correct way - it does not block your main thread, so UI stays responsive:
将 Timer 与 TimerTask。请参阅此处的示例
Use Timer in conjunction with TimerTask. See examples here