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 i开发者_开发技巧d;
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();
}
}
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.
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
Use Timer in conjunction with TimerTask. See examples here
精彩评论