在ormlite官方的demo中,Activity 中访问数据库是extends ormliteBaseActivity.
那在Fragment中怎么使用呢?
简单的:
public class OrmLiteFragment extends Fragment { private DatabaseHelper databaseHelper = null; protected DatabaseHelper getHelper() { if (databaseHelper == null) { databaseHelper = OpenHelperManager.getHelper(getActivity(), DatabaseHelper.class); } return databaseHelper; } @Override public void onDestroy() { super.onDestroy(); if (databaseHelper != null) { OpenHelperManager.releaseHelper(); databaseHelper = null; } } }
模ormliteBaseActivity
/** * 只能在OnStart 以后的生命周期中调用。 * * @author Administrator * * @param <H> */ public class OrmBaseLiteFragment <H extends OrmLiteSqliteOpenHelper> extends Fragment { private volatile H helper; private volatile boolean created = false; private volatile boolean destroyed = false; private static Logger logger = LoggerFactory.getLogger(OrmBaseLiteFragment.class); /** * Get a helper for this action. */ public H getHelper() { if (helper == null) { if (!created) { throw new IllegalStateException("A call has not been made to onCreate() yet so the helper is null"); } else if (destroyed) { throw new IllegalStateException( "A call to onDestroy has already been made and the helper cannot be used after that point"); } else { throw new IllegalStateException("Helper is null for some unknown reason"); } } else { return helper; } } /** * Get a connection source for this action. */ public ConnectionSource getConnectionSource() { return getHelper().getConnectionSource(); } // /** // * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! // * // * // */ // @Override // public View onCreateView(LayoutInflater inflater , ViewGroup container , Bundle savedInstanceState) // { // if (helper == null) { // helper = getHelperInternal(getActivity()); // created = true; // } // super.onCreate(savedInstanceState); // return container; // } @Override public void onStart() { super.onStart(); if (helper == null) { LogUtil.e("==============ormFragment", "getHelperInternal in ormFragment"); helper = getHelperInternal(getActivity()); created = true; } } @Override public void onDestroy() { super.onDestroy(); releaseHelper(helper); destroyed = true; } /** * This is called internally by the class to populate the helper object instance. This should not be called directly * by client code unless you know what you are doing. Use {@link #getHelper()} to get a helper instance. If you are * managing your own helper creation, override this method to supply this activity with a helper instance. * * <p> * <b> NOTE: </b> If you override this method, you most likely will need to override the * {@link #releaseHelper(OrmLiteSqliteOpenHelper)} method as well. * </p> */ protected H getHelperInternal(Context context) { @SuppressWarnings({ "unchecked", "deprecation" }) H newHelper = (H) OpenHelperManager.getHelper(context); logger.trace("{}: got new helper {} from OpenHelperManager", this, newHelper); return newHelper; } /** * Release the helper instance created in {@link #getHelperInternal(Context)}. You most likely will not need to call * this directly since {@link #onDestroy()} does it for you. * * <p> * <b> NOTE: </b> If you override this method, you most likely will need to override the * {@link #getHelperInternal(Context)} method as well. * </p> */ protected void releaseHelper(H helper) { OpenHelperManager.releaseHelper(); logger.trace("{}: helper {} was released, set to null", this, helper); this.helper = null; } @Override public String toString() { return getClass().getSimpleName() + "@" + Integer.toHexString(super.hashCode()); } }
时间: 2024-12-19 13:24:28