2013-08-12 1 views
0

En ce moment, j'appelle une fonction insertSomeContacts() dans la méthode onCreate de MainActivity qui ajoute évidemment les contacts donnés chaque fois que l'application est redémarrée (y compris le roat screen) ... depuis mon SQLiteOpenHelper La sous-classe fait partie de ma classe ContactsDBAdapter (qui porte la méthode insertSomeContacts()) - comment est-ce que j'exécute cette fonction dans SQLiteOpenHelper onCreate afin qu'elle ne s'exécute qu'une fois lors de la création de la base de données?Accédez à SQLiteOpenHelper onCreate Méthode de la classe d'habillage

Vous avez vraiment des problèmes pour comprendre l'étendue de ce problème et pour passer correctement cette portée.

MainActivity.java:

public class MainActivity extends Activity { 

Intent intent; 

private ContactsDBAdapter dbHelper; 
private SimpleCursorAdapter dataAdapter; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    dbHelper = new ContactsDBAdapter(this); 
    dbHelper.open(); 

    //dbHelper.deleteAll(); 
    //dbHelper.insertSomeContacts(); 
    displayListView(); 
} 

private void displayListView(){ 

    Cursor cursor = dbHelper.getAll(); 

    String[] fromColumns = new String[]{ 
      ContactsDBAdapter.COLUMN_TYPE, 
      ContactsDBAdapter.COLUMN_CLTYP, 
      ContactsDBAdapter.COLUMN_NAME, 
      ContactsDBAdapter.COLUMN_VNAME 
    }; 

    int[] toViews = new int[]{ 
     R.id.contactType, 
     R.id.contactCltype, 
     R.id.contactName, 
     R.id.contactVname 
    }; 

    dataAdapter = new SimpleCursorAdapter(this, R.layout.contact_entry, cursor, fromColumns, toViews, 0); 

    ListView listview = (ListView) findViewById(R.id.list); 
    listview.setAdapter(dataAdapter); 

} 

ContactsDBAdapter.java:

public class ContactsDBAdapter{ 

public static final String COLUMN_ID = "_id"; 
public static final String COLUMN_TYPE = "type"; 
public static final String COLUMN_CLTYP = "cltyp"; 
public static final String COLUMN_MDT = "mdt"; 
public static final String COLUMN_OBJ = "obj"; 
public static final String COLUMN_VTR = "vtr"; 
public static final String COLUMN_FKZ = "fkz"; 
public static final String COLUMN_NAME = "name"; 
public static final String COLUMN_VNAME = "vname"; 
public static final String COLUMN_TEL = "tel"; 
public static final String COLUMN_FAX = "fax"; 
public static final String COLUMN_MOBIL = "mobil"; 
public static final String COLUMN_EMAIL = "email"; 

private static final String TAG = "ContactsDBAdapter"; 
private DBTools mDbHelper; 
private SQLiteDatabase mDb; 

private static final String DATABASE_NAME = "hvkontakte.db"; 
private static final String DATABASE = "hvkontakte"; 
private static final String TABLE_NAME = DATABASE; 
private static final int DATABASE_VERSION = 1; 

private final Context mCtx; 

private static final String DATABASE_CREATE = 
     "CREATE TABLE " + TABLE_NAME + "(" + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + 
       COLUMN_TYPE + ", " + COLUMN_CLTYP + ", " + COLUMN_MDT + ", " + COLUMN_OBJ + ", " 
       + COLUMN_VTR + ", " + COLUMN_FKZ + ", " + COLUMN_NAME + ", " + COLUMN_VNAME + ", " 
       + COLUMN_TEL + ", " + COLUMN_FAX + ", " + COLUMN_MOBIL + ", " + COLUMN_EMAIL + ")"; 

private static class DBTools extends SQLiteOpenHelper{ 

    public DBTools(Context context){ 
     super(context, DATABASE_NAME, null, DATABASE_VERSION); 
    } 

    @Override 
    public void onCreate(SQLiteDatabase database) { 
     Log.w(TAG, DATABASE_CREATE); 
     database.execSQL(DATABASE_CREATE); 
    } 

    @Override 
    public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) { 
     database.execSQL("DROP TABLE IF EXISTS" + DATABASE); 
     onCreate(database); 
    } 
} 

public ContactsDBAdapter(Context ctx){ 
    this.mCtx = ctx; 
} 

public ContactsDBAdapter open() throws SQLException{ 
    mDbHelper = new DBTools(mCtx); 
    mDb = mDbHelper.getWritableDatabase(); 
    return this; 
} 

public void close(){ 
    if(mDbHelper != null){ 
     mDbHelper.close(); 
    } 
} 

public long createContact(String type, String cltyp, String mdt, String obj, String vtr, 
          String fkz, String name, String vname, String tel, String fax, 
          String mobil, String email) { 

      ContentValues initialValues = new ContentValues(); 
      initialValues.put(COLUMN_TYPE, type); 
      initialValues.put(COLUMN_CLTYP, cltyp); 
      initialValues.put(COLUMN_MDT, mdt); 
      initialValues.put(COLUMN_OBJ, obj); 
      initialValues.put(COLUMN_VTR, vtr); 
      initialValues.put(COLUMN_FKZ, fkz); 
      initialValues.put(COLUMN_NAME, name); 
      initialValues.put(COLUMN_VNAME, vname); 
      initialValues.put(COLUMN_TEL, tel); 
      initialValues.put(COLUMN_FAX, fax); 
      initialValues.put(COLUMN_MOBIL, mobil); 
      initialValues.put(COLUMN_EMAIL, email); 

      return mDb.insert(TABLE_NAME, null, initialValues); 
     } 

public boolean deleteAll() { 
    int doneDelete = 0; 
    doneDelete = mDb.delete(TABLE_NAME, null , null); 
    return doneDelete > 0; 
} 

public Cursor getAll() { 
    Cursor mCursor = mDb.query(TABLE_NAME, new String[] {COLUMN_ID, 
      COLUMN_TYPE, COLUMN_CLTYP, COLUMN_NAME, COLUMN_VNAME}, 
      null, null, null, null, null); 

    if (mCursor != null) { 
    mCursor.moveToFirst(); 
    } 
    return mCursor; 
} 

public void insertSomeContacts(){ 
    createContact("vtr","Tenant","1","82","1","2","Bennett","Tony","0911-123456","0911-123457","01577-12345678","[email protected]"); 
    createContact("vtr","Owner","1","82","","","Smith","Brad","0911-1234567","0911-1234567","01577-84368365","[email protected]"); 
    //createContact("","","","","","","","","","","",""); 
    //createContact("","","","","","","","","","","",""); 
    //createContact("","","","","","","","","","","",""); 
    //createContact("","","","","","","","","","","",""); 

} 

}

+0

fonctionne avec SharedPreferences. Définissez un booléen init. Si init est faux, insérez les contacts -> mettez l'init à true. après avoir redémarré en notant quand init est vrai. –

+0

pouvez-vous afficher ceci comme une réponse –

Répondre

1

travail avec SharedPreferences. Définissez un init boolean. Si init est faux, insérez les contacts -> mettez l'init à true. après avoir redémarré en notant quand init est vrai.

Mais ce n'est peut-être pas la meilleure solution pour votre utilisation ...

+0

True. Je ne voudrais pas enregistrer plus d'une poignée de paramètres de cette façon, mais il est persistant et fait le travail pour l'instant. Vielen Dank. –

+0

Kein Problème :) –

Questions connexes