Android осень 2013 лекция 6

  • View
    315

  • Download
    3

Embed Size (px)

Transcript

  • 1.SQlite Open source Qt, Skype, iOS . http://www.sqlite.org/lang.ht ml2

2. Database in Android android.database.sqlite Schema Contract SQLiteDatabase SQLiteOpenHelper _ID3 3. Database Contract4 4. Use Contract5 5. SQLiteOpenHelper6 6. Insert7 7. Query8 8. Update9 9. Cursors Virtual Table moveToFirst() close()10 10. Loaders 3.0( support library) : : LoaderManager LoaderCallbacks Loader CursorLoader AsyncTaskLoader 11 11. LoaderCallbacks public class SampleActivity extends Activity implements LoaderManager.LoaderCallbacks { public Loader onCreateLoader(int id, Bundle args) { ... } public void onLoadFinished(Loader loader, D data) { ... } public void onLoaderReset(Loader loader) { ... } /* ... */ } 12 12. Simple Loader Example public class SampleListActivity extends ListActivity implements LoaderManager.LoaderCallbacks { private static final String[] PROJECTION = new String[] { "_id", "text_column" }; private static final int LOADER_ID = 1; private LoaderManager.LoaderCallbacks mCallbacks; private SimpleCursorAdapter mAdapter;@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); String[] dataColumns = { "text_column" }; int[] viewIDs = { R.id.text_view };mAdapter = new SimpleCursorAdapter(this, R.layout.list_item, null, dataColumns, viewIDs, 0);setListAdapter(mAdapter);mCallbacks = this; LoaderManager lm = getLoaderManager(); lm.initLoader(LOADER_ID, null, mCallbacks); }13 13. Simple Loader Example @Override public Loader onCreateLoader(int id, Bundle args) { return new CursorLoader(SampleListActivity.this, CONTENT_URI, PROJECTION, null, null, null); } @Override public void onLoadFinished(Loader loader, Cursor cursor) { switch (loader.getId()) { case LOADER_ID:mAdapter.swapCursor(cursor); break; } }@Override public void onLoaderReset(Loader loader) { mAdapter.swapCursor(null); }14 14. Implementing Loaders Event-driven : AsyncTask Listener : Started Stopped Reset 15 15. ContentProvider16 16. ContentProvider CRUD URI Batch Operations Permissions ContentResolver.query , ? 17 17. URI : authority : path content://user_dictionary/words Uri singleUri = ContentUris.withAppendedId(UserDictionary.Words.C ONTENT_URI,4);18