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();
}
LogCat error :
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
at android.os.Handler.<init>(Handler.java:200)
at android.os.Handler.<init>(Handler.java:114)
at android.widget.Toast$TN.<init>(Toast.java:327)
at android.widget.Toast.<init>(Toast.java:92)
at android.widget.Toast.makeText(Toast.java:241)
at joancolmenero.com.practicabasedadesxmlthreads.MainActivity.NumeroDNI(MainActivity.java:111)
at joancolmenero.com.practicabasedadesxmlthreads.MainActivity.crearRegistres(MainActivity.java:123)
at joancolmenero.com.practicabasedadesxmlthreads.MainActivity$1.run(MainActivity.java:41)
at java.lang.Thread.run(Thread.java:841)
03-26 11:49:20.436 21310-21310/joancolmenero.com.practicabasedadesxmlthreads E/WindowManager﹕ android.view.WindowLeaked: Activity joancolmenero.com.practicabasedadesxmlthreads.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{41f8f5b8 V.E..... R......D 0,0-684,324} that was originally added here
at android.view.ViewRootImpl.<init>(ViewRootImpl.java:361)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:248)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
at android.app.Dialog.show(Dialog.java:286)
at android.app.ProgressDialog.show(ProgressDialog.java:116)
at android.app.ProgressDialog.show(ProgressDialog.java:99)
at joancolmenero.com.practicabasedadesxmlthreads.MainActivity.onCreate(MainActivity.java:29)
at android.app.Activity.performCreate(Activity.java:5248)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2173)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2269)
at android.app.ActivityThread.access$800(ActivityThread.java:139)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5102)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
EDIT2
When I run the APP it takes like 5 or 6 seconds to show my activity, it shows a white screen with anything ...
public class MainActivity extends ActionBarActivity {
Button test;
public SQLiteDatabase db;
public ClientsSQL Conexion;
ProgressDialog progress;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Conexion = new ClientsSQL(this, "Clients", null, 1);
db = Conexion.getReadableDatabase();
new MyAsyncTask().execute();
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);
c.close();
}
private class MyAsyncTask extends AsyncTask<Void, Void, Void> {
private final ProgressDialog dialog = new ProgressDialog(MainActivity.this);
@Override
protected void onPreExecute() {
this.dialog.setMessage("Cargando DNI en SQLite");
this.dialog.show();
}
@Override
protected Void doInBackground(Void... params) {
db.beginTransaction();
try {
crearRegistres(db);
db.setTransactionSuccessful();
} catch (Exception ignored) {
} finally {
db.endTransaction();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
Cursor c;
c = db.rawQuery("Select NIF from Clients where NIF", null);
Toast.makeText(MainActivity.this, Integer.toString(c.getCount()), Toast.LENGTH_LONG).show();
c.close();
this.dialog.dismiss();
}
}
}