0

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

}
iknow
  • 8,358
  • 12
  • 41
  • 68
moe
  • 247
  • 3
  • 8
  • 21

3 Answers3

1

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.

pzulw
  • 1,716
  • 15
  • 22
0

Use Timer in conjunction with TimerTask. See examples here

Community
  • 1
  • 1
ernazm
  • 9,208
  • 4
  • 44
  • 51
0

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
pecka85
  • 752
  • 7
  • 21
  • Although, the Runnable execution does block the thread you created the Handler on, typically the main thread, and executes your runnable code on that looper. – milosmns Feb 06 '17 at 16:53