Create database using splash screen ANDROID

0

I have a splash screen in which I try to verify the existence of a database and create it in the case of not existing but I do not know how to call the methods of the class AppSQLiteHelper.java as I could do it? In addition, my intention is that as the checks increase, the bar of the progress bar will increase until reaching a hypothetical 100% in which it would jump to the main activity. I attach the code that I have so far.

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

public class AppSQLiteHelper extends SQLiteOpenHelper{

//Sentencia SQL para crear las tablas
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) {
    //Se ejecuta las sentencias SQL de creación de las tablas
    db.execSQL(sqlCreateCartera);
    db.execSQL(sqlCreateValor);
    db.execSQL(sqlCreateEstado);
}

@Override
public void onUpgrade(SQLiteDatabase db, int versionAnterior, int versionNueva) {
    //NOTA: Por simplicidad del ejemplo aquí utilizamos directamente la opción de
    //      eliminar la tabla anterior y crearla de nuevo vacía con el nuevo formato.
    //      Sin embargo lo normal será que haya que migrar datos de la tabla antigua
    //      a la nueva, por lo que este método debería ser más elaborado.

    //Se elimina la versión anterior de las tablas
    db.execSQL("DROP TABLE IF EXISTS CARTERA");
    db.execSQL("DROP TABLE IF EXISTS VALOR");
    db.execSQL("DROP TABLE IF EXISTS ESTADO");

    //Se crea la nueva versión de las tablas
    db.execSQL(sqlCreateCartera);
    db.execSQL(sqlCreateValor);
    db.execSQL(sqlCreateEstado);
}

}

Any help is welcome; D

    
asked by Eduardo 22.04.2017 в 23:38
source

1 answer

1

To refer to your class AppSQLiteHelper you must have two variables:

AppSQLiteHelper dbHelper;
SQLiteDatabase  basededatos; 

In the same onCreate of your SplashScreenActivity you initialize them:

@Override
protected void onCreate(Bundle savedInstanceState) {
    dbHelper = new AppSQLiteHelper (this,"nombre", null, 1);  
    basededatos = dbHelper.getWritableDatabase(); 
}

And the methods you can use to make SELECT or INSERT are the following (customize them to your needs):

public Cursor select() {
   String[] cols = new String[] {"COL1", "COL2"};  
   Cursor mCursor = basededatos.query(true, "TABLA",cols,null  
            , null, null, null, null, null);  
   if (mCursor != null) {  
     mCursor.moveToFirst();  
   }  
   return mCursor; // Recorrer para recoger cada valor
}

public long insertar(String valor1, String valor2){  
   ContentValues valores = new ContentValues();  
   valores.put("COL1", valor1);  
   valores.put("COL2", valor2);  
   return basededatos.insert("TABLA", null, valores);  
}    
    
answered by 24.04.2017 / 10:29
source