简体   繁体   English

在Android启动画面中创建数据库

[英]Create database in Android splash-screen

I have a splash screen in my app and im triying to check if the tables of my db exist and if not, create the tables that my app needs. 我的应用程序中有一个初始屏幕,试图检查数据库表是否存在,如果不存在,请创建应用程序所需的表。 My idea is to make 2 checks, something like this: 我的想法是进行2次检查,如下所示:

if(db.exists){
increase the progressbar to the 100%
(If we could add a short pause to the progress bar transition better, just 
for a better user experience)}

else{
increase the progress bar to 50% and create databases. By last increase 
progress bar to 100%}

start -> main_activity

This just the idea that i have but i dont how to make and i dont how to reference the methods of AppSQLiteHelper.java. 这只是我的想法,但我不知道如何制作和引用AppSQLiteHelper.java的方法。 If any one of you have a better idea for improve the code logic better. 如果您有更好的想法可以更好地改进代码逻辑。 Here is the code that write till the moment: 这是到目前为止编写的代码:

SplashScreenActivity.java: SplashScreenActivity.java:

public class SplashScreenActivity extends Activity {

// Set the duration of the splash screen
public ProgressBar splash_screenProgressBar;
public int MAX_VALUE = 30;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Set portrait orientation
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    setContentView(R.layout.splash_screen);

    splash_screenProgressBar = (ProgressBar) findViewById(R.id.splash_screenProgressBar);
    splash_screenProgressBar.setMax(MAX_VALUE);

    new CountDownTimer(3000, 100) {

        int progreso = 1; // Variable que va a ir aumentando del progreso
        @Override
        public void onTick(long millisUntilFinished) {
            splash_screenProgressBar.setProgress(progreso);
            progreso += (1);
        }

        @Override
        public void onFinish() {
            splash_screenProgressBar.setProgress(MAX_VALUE);

            // Start the next activity
            Intent mainIntent = new Intent().setClass(SplashScreenActivity.this, MainActivity.class);
            startActivity(mainIntent);

            // Close the activity so the user won't able to go back this activity pressing Back button
            finish();
        }
    }.start();
}

}

AppSQLiteHelper.java: AppSQLiteHelper.java:

public class AppSQLiteHelper extends SQLiteOpenHelper{

String sqlCreateCartera = "CREATE TABLE CARTERA (saldo INTEGER)";
String sqlCreateValor = "CREATE TABLE VALOR (cantidad INTEGER, precio_accion INTEGER, entidad TEXT)";
String sqlCreateEstado = "CREATE TABLE ESTADO (estado BOOLEAN)";

public AppSQLiteHelper(Context contexto, String nombre,
                            CursorFactory factory, int version) {
    super(contexto, nombre, factory, version);
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(sqlCreateCartera);
    db.execSQL(sqlCreateValor);
    db.execSQL(sqlCreateEstado);
}

@Override
public void onUpgrade(SQLiteDatabase db, int versionAnterior, int versionNueva) {
    db.execSQL("DROP TABLE IF EXISTS CARTERA");
    db.execSQL("DROP TABLE IF EXISTS VALOR");
    db.execSQL("DROP TABLE IF EXISTS ESTADO");

    db.execSQL(sqlCreateCartera);
    db.execSQL(sqlCreateValor);
    db.execSQL(sqlCreateEstado);
}

}

Why do want to check for DB if exist or not because as soon as you create your object of AppSQLiteHelper .. it automatically calls the overridden function of onCreate() to initialize the DB you dont have to explicitly check if its exist or not.If you really want to do this inspite of above conditions then you can create a callback interface and implement in you helper class and after your oncreate execute you can check whether its created a db.Like below.. 为什么要检查数据库是否存在,因为一旦创建AppSQLiteHelper对象,它就会自动调用onCreate()的重写函数来初始化数据库,而不必显式检查数据库是否存在。如果确实有上述条件,您仍然确实想这样做,那么您可以创建一个回调接口并在您的帮助器类中实现,在oncreate执行之后,您可以检查其是否在下面创建了db.Like。

public interface OnDBInitializeListener {
    public void onCreateCalled(int position);
}

and use this while you call your helper class by passing this on constructor..Hope this helps 并在将其传递给构造函数时调用帮助程序类时使用此方法。希望有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM