Search code examples
androidsqlitedelay

Android SQLite delay


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();        
    } 

}

Solution

  • 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