Commit 8589c065 authored by Lucas NAURY's avatar Lucas NAURY

Initialisation de la DB si vide

parent 63d33379
......@@ -26,11 +26,11 @@ public class DBHelper extends SQLiteOpenHelper {
static final String DB_NAME = "LEBONCOIN.DB";
// database version
static final int DB_VERSION = 1;
static final int DB_VERSION = 2;
// Creating table query
private static final String CREATE_TABLE = "create table " + TABLE_NAME + "(" + _ID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + TITLE + " TEXT NOT NULL, " + ADDRESS + " TEXT, " + IMAGE + " TEXT, " + PRICE + " DOUBLE, " + DESCRIPTION + "TEXT);";
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + TITLE + " TEXT NOT NULL, " + ADDRESS + " TEXT, " + IMAGE + " TEXT, " + PRICE + " DOUBLE, " + DESCRIPTION + " TEXT);";
public DBHelper(Context context) {
super(context,
......
......@@ -24,7 +24,6 @@ public class DBManager {
private DBManager(Context c) {
context = c;
//init(); // Useful for adding ads for the first time.
}
public static DBManager getDBManager(Context context) {
......@@ -37,6 +36,12 @@ public class DBManager {
public DBManager open() throws SQLException {
dbHelper = new DBHelper(context);
database = dbHelper.getWritableDatabase();
//S'il n'y a pas d'annonce, on initialise une liste d'annonces
if(this.getAll().size() == 0){
init();
}
return this;
}
......@@ -100,17 +105,15 @@ public class DBManager {
Cursor cursor = this.fetch();
if (cursor.moveToFirst()) {
do {
String titre = cursor.getString(abs(cursor.getColumnIndex(DBHelper.TITLE)));
String adresse = cursor.getString(abs(cursor.getColumnIndex(DBHelper.ADDRESS)));
String image = cursor.getString(abs(cursor.getColumnIndex(DBHelper.IMAGE)));
double prix = cursor.getDouble(abs(cursor.getColumnIndex(DBHelper.PRICE)));
String description = cursor.getString(abs(cursor.getColumnIndex(DBHelper.DESCRIPTION)));
do {
String titre = cursor.getString(abs(cursor.getColumnIndex(DBHelper.TITLE)));
String adresse = cursor.getString(abs(cursor.getColumnIndex(DBHelper.ADDRESS)));
String image = cursor.getString(abs(cursor.getColumnIndex(DBHelper.IMAGE)));
double prix = cursor.getDouble(abs(cursor.getColumnIndex(DBHelper.PRICE)));
String description = cursor.getString(abs(cursor.getColumnIndex(DBHelper.DESCRIPTION)));
annonces.add(new Annonce(titre, adresse, image, prix, description));
} while (cursor.moveToNext());
}
annonces.add(new Annonce(titre, adresse, image, prix, description));
} while (cursor.moveToNext());
return annonces;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment