我想从数据库中提取一个值并在倒数计时器中使用它。似乎它不可能与CountDownTimer一起使用,但看起来它可以通过Handler实现。任何人都可以提供一个例子吗?
我正在使用Android Studio
由于
这是我的计时器课程:
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.widget.TextView;
/**
* Created by Mat on 14-Feb-17.
*/
public class Timer extends Activity{
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
textView = (TextView) findViewById(R.id.text);
// 15000 is the starting number (in milliseconds)
// 1000 is the number to count down each time (in milliseconds)
MyCount counter = new MyCount(DisplayTime2(), 1000); // <---
counter.start();
}
private long DisplayTime2(Cursor d) { // <------------
return (d.getLong(2));
}
// count down timer is an abstract class, so extend it and fill in methods
public class MyCount extends CountDownTimer {
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onFinish() {
// it's done
textView.setText("Done!");
}
@Override
public void onTick(long millisUntilFinished) {
textView.setText("Left: " + millisUntilFinished / 1000);
}
}
}
这是我的DatabaseAccess类
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.util.ArrayList;
import java.util.List;
public class DatabaseAccess {
private SQLiteOpenHelper openHelper;
private SQLiteDatabase database;
private static DatabaseAccess instance;
private static String DATABASE_TABLE = "Cardio";
static final String KEY_ROWID = "rowid";
static final String KEY_NAME = "Exercise";
/**
* Private constructor to aboid object creation from outside classes.
*
* @param context
*/
public DatabaseAccess(Context context) {
this.openHelper = new DatabaseOpenHelper(context);
}
/**
* Return a singleton instance of DatabaseAccess.
*
* @param context the Context
* @return the instance of DabaseAccess
*/
public static DatabaseAccess getInstance(Context context) {
if (instance == null) {
instance = new DatabaseAccess(context);
}
return instance;
}
/**
* Open the database connection.
*/
public void open() {
this.database = openHelper.getWritableDatabase();
}
/**
* Close the database connection.
*/
public void close() {
if (database != null) {
this.database.close();
}
}
/**
* Read all quotes from the database.
*
* @return a List of quotes
*/
public List<String> getQuotes() {
List<String> list = new ArrayList<>();
Cursor cursor = database.rawQuery("SELECT * FROM quotes", null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
list.add(cursor.getString(0));
cursor.moveToNext();
}
cursor.close();
return list;
}
public Cursor getRProgram() {
Cursor mCursor= database.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_NAME},null,null,null,null,"RANDOM()");
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
}
这是我的MainActivity
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.List;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.List;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button generate = (Button) findViewById(R.id.Generate);
generate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
DatabaseAccess myDatabaseHelper = new DatabaseAccess(MainActivity.this);
myDatabaseHelper.open();
Cursor d = myDatabaseHelper.getRProgram();
if (d.moveToFirst()) ;
DisplayProgram(d);
myDatabaseHelper.close();
}
public void DisplayProgram(Cursor d) {
TextView result2 = (TextView) findViewById(R.id.result2);
result2.setText("\n" + d.getString(1));
}
});
}
}
答案 0 :(得分:0)
以下是从15到0的倒计时示例。您可以获得所需的值并将其传递到我使用15000(毫秒)的地方
public class YourClass extends Activity {
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
textView = (TextView) findViewById(R.id.text);
// 15000 is the starting number (in milliseconds)
// 1000 is the number to count down each time (in milliseconds)
MyCount counter = new MyCount(15000, 1000); // PASS YOUR VALUE INSTEAD 15000 Here
counter.start();
}
// count down timer is an abstract class, so extend it and fill in methods
public class MyCount extends CountDownTimer {
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onFinish() {
// it's done
textView.setText("Done!");
}
@Override
public void onTick(long millisUntilFinished) {
textView.setText("Left: " + millisUntilFinished / 1000);
}
}
}