0

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

确实在理解 this 的范围并正确传递该范围时遇到了问题。

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

}

}

4

1 回答 1

1

SharedPreferences. 设置一个 init boolean。如果 init 为 false,则插入联系人 -> 将 init 设置为 true。重新启动后,请注意 init 何时为真。

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

于 2013-08-12T15:11:14.110 回答