从包装类访问SQLiteOpenHelper onCreate方法

时间:2013-08-12 14:37:44

标签: java android scope sharedpreferences oncreate

现在我在MainActivity的onCreate方法中调用insertSomeContacts()函数,显然每次重启应用程序时都会添加给定的联系人(包括屏幕旋转)...因为我的SQLiteOpenHelper子类是我的一部分ContactsDBAdapter类(它带有insertSomeContacts()方法) - 如何在SQLiteOpenHelper onCreate中执行此函数,以便它只在创建数据库时执行一次?

在理解这个范围并且正确地传递该范围时确实遇到了问题。

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("","","","","","","","","","","","");

}

}

1 个答案:

答案 0 :(得分:1)

SharedPreferences合作。设置初始值boolean。如果init为false,则插入联系人 - >将init设置为true。重新启动后请注意init为真。

但这可能不是您用例的最佳解决方案......