简体   繁体   中英

Access SQLiteOpenHelper onCreate Method from wrapping class

Right now i am calling an insertSomeContacts() function in the onCreate method of the MainActivity which obviously adds the given contacts every time the app is restarted (including screen roation)...since my SQLiteOpenHelper Sub-Class is part of my ContactsDBAdapter Class (which carries the insertSomeContacts() method) - how do i get this function to execute in the SQLiteOpenHelper onCreate so that it only executes once at creation of the database?

Really having problems understanding the scope of this and passing that scope around properly.

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","info@email.com");
    createContact("vtr","Owner","1","82","","","Smith","Brad","0911-1234567","0911-1234567","01577-84368365","info@email.com");
    //createContact("","","","","","","","","","","","");
    //createContact("","","","","","","","","","","","");
    //createContact("","","","","","","","","","","","");
    //createContact("","","","","","","","","","","","");

}

}

work with SharedPreferences . Set a init boolean . if init is false insert the contacts -> set the init to true. after restarting do noting when init is true.

But this is maybe not the best solution for your usecase...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM