我试图从SQLite中获取一个值,转换它并在Android应用程序的倒数计时器中使用它,但我得到了意想不到的结果。我打算只查询一次数据库,获取数据并使用它而无需再次返回数据库。
但是,每次启动活动时,都会再次查询数据库,并返回相同的旧值,使倒计时从头开始。我做得不对劲?有任何想法吗?这是我的代码:
public class PyStatus extends ListActivity {
public pyCountDown timer;
private long startTime = 0L;
private final long interval = 1000;
private TextView mDaysLeft;
private PyDbAdapter mDbHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.py_status);
mDbHelper = new PyDbAdapter(this);
mDbHelper.open();
mDaysLeft = (TextView) findViewById(R.id.days_leftTv);
doDataConversion(); // call this method to run the countdown timer
mDaysLeft.setText(mDaysLeft.getText() + String.valueOf(startTime));
}
private void doDataConversion() {
Cursor daysLeftCursor = mDbHelper.findDaysLeft();
if (daysLeftCursor.moveToFirst()) {
int columnIndex = daysLeftCursor.getColumnIndex(PyDbAdapter.COL_DAYSLEFT);
String daysToD = daysLeftCursor.getString(columnIndex);
long stt = Long.parseLong(daysToD);
startTime = stt;
timer = (pyCountDown) new pyCountDown(startTime, interval).start();
}
}
private class pyCountDown extends CountDownTimer {
public pyCountDown(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onTick(long millisUntilFinished) {
mDaysLeft.setText("Days Left: " + millisUntilFinished/(1000));
}
@Override
public void onFinish() {
mDaysLeft.setText("Done... Go home!");
}
}
答案 0 :(得分:0)
如果您只想使用static
变量来保存应用程序运行的时间应该可以解决问题。它们一直存在,直到父类卸载为止。
public class PyStatus extends ListActivity {
public pyCountDown timer;
private long startTime = 0L;
private final long interval = 1000;
private TextView mDaysLeft;
private PyDbAdapter mDbHelper;
private static cursor daysLeftCursor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.py_status);
mDbHelper = new PyDbAdapter(this);
mDbHelper.open();
mDaysLeft = (TextView) findViewById(R.id.days_leftTv);
doDataConversion(); // call this method to run the countdown timer
mDaysLeft.setText(mDaysLeft.getText() + String.valueOf(startTime));
}
private void doDataConversion() {
if (daysLeftCursor == null)
daysLeftCursor = mDbHelper.findDaysLeft();
if (daysLeftCursor.moveToFirst()) {
int columnIndex = daysLeftCursor.getColumnIndex(PyDbAdapter.COL_DAYSLEFT);
String daysToD = daysLeftCursor.getString(columnIndex);
long stt = Long.parseLong(daysToD);
startTime = stt;
timer = (pyCountDown) new pyCountDown(startTime, interval).start();
}
}
private class pyCountDown extends CountDownTimer {
public pyCountDown(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onTick(long millisUntilFinished) {
mDaysLeft.setText("Days Left: " + millisUntilFinished / (1000));
}
@Override
public void onFinish() {
mDaysLeft.setText("Done... Go home!");
}
}
}
注意: 如果要在应用程序关闭时保存,则需要在活动暂停或停止时将当前时间保存回数据库,具体取决于您的选择。以下是有关Activity's Lifecycle的信息的链接。
答案 1 :(得分:0)
我想要的一个出路就是:
使用Hashmpa存储数据库数据,仅当hashmap为空或者hashmap中没有所需数据时才查询数据库。
所以你的代码首先检查空hashmap,然后再检查hashmap中是否存在所需的数据....
如果两个条件都失败,那么你查询数据库,否则你不会查询...所以你的查询语句应该在if
内,如果失败则访问数据库,否则只使用hashmap数据。
希望这有帮助。