jeudi 26 mars 2015

Android - How to handle 1000 records SQLite

I'm trying to add 1000 records on my SQLite at start, but the thing is that when I launch the APP it turns white screen, then black screen until it crashes, when I put like 5 records, it still doing the same with the white screen and black screen, but when adds the records, it show me a toast with all of the records, so it works... This is my SQLiteBaseAdapter :



public class ClientsSQL extends SQLiteOpenHelper {
// Declaramos la sentencia para crear la tabla
String sqlCreate = "CREATE TABLE Clients(NIF text,cognom1 text,cognom2 text,nom text)";



public ClientsSQL(Context context, String name, SQLiteDatabase.CursorFactory factory,
int version) {
super(context, name, factory, version);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {

db.execSQL(sqlCreate);
db.execSQL("INSERT INTO Clients VALUES ('39410028B', 'hola1', 'Hola2', 'Jonathan')");
Log.v("INFO1", "creating db");


}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
// Si existe la tabla, la borramos y la volvemos a crear.
db.execSQL("DROP TABLE IF EXISTS Clients");
db.execSQL(sqlCreate);
}


I created 1 insert as a test, but then on my MainActivity, I did this :



public void crearRegistres(SQLiteDatabase db){

int i;
String dni ="";

for (i = 0; i < 1000; i++) {

dni = NumeroDNI();

db.execSQL("INSERT INTO Clients VALUES ('" + dni + "'," + "'cognomA" + i + "'," + "'cognomB" + i + "',"+ "'nom" + i + "')");
}
Cursor c;
c = db.rawQuery("Select NIF from Clients", null);


Toast.makeText(MainActivity.this, Integer.toString(c.getCount()),
Toast.LENGTH_LONG).show();
c.close();
}


I tried to create an Async class, but don't get it, how I call the doInBackGround() method (if it's the way to do it properly...).


EDIT1


This is what I tried, but I can't do nothing, until it finish.



progress = ProgressDialog.show(getActivity(), "Cargando",
"Espere mientras cargan sus ofertas", true);
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(2000);


} catch (InterruptedException e) {
e.printStackTrace();
}

getActivity().runOnUiThread(new Runnable() {

@Override
public void run() {
crearRegistres(db);
progress.dismiss();
}
});
}

}).start();
}

Aucun commentaire:

Enregistrer un commentaire