Android SQLite 延迟

发布于 2024-11-02 11:47:56 字数 1559 浏览 1 评论 0原文

我有这个代码,这是我找到的一个例子。在此代码中,我插入 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 技术交流群。

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

发布评论

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

评论(3

路还长,别太狂 2024-11-09 11:47:56

您不应该将任何长时间运行的函数放在主线程中。 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.

笑红尘 2024-11-09 11:47:56

正确的方法 - 它不会阻塞您的主线程,因此 UI 保持响应:

Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
    // Get new entry
}, 5000);  // 5000 miliseconds

The correct way - it does not block your main thread, so UI stays responsive:

Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
    // Get new entry
}, 5000);  // 5000 miliseconds
就此别过 2024-11-09 11:47:56

TimerTimerTask。请参阅此处的示例

Use Timer in conjunction with TimerTask. See examples here

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