Android - using ORMLite DAO as ContentProvider

I saw sample C in a presentation by Google IO, and I really want to implement this template. However, I really like the ORMLite library and I want to use this library in my application.

When I say a Google IO presentation, I mean the following: https://www.youtube.com/watch?v=xHXn3Kg2IQE Virgil Dobzanski

Now I have been looking a lot for an implementation that shows me how to use ORMLite in combination with Contentproviders.

Now my problem is that the ORMLite DAO is contrary to the Contentprovider. They essentially do the same and are a pain for integration into each other. ( Using Ormlite in conjunction with the Android content provider, others discuss this and agree with this statement.)

Several libraries have implemented ORMLite in the contentprovider API template, one example: https://github.com/blandware/android-atleap

However, under water, they still return the ContentValues ​​model (simple types).

Android - using a Dao template using contentProvider This question is similar to my situation, but 3 years ago, and I propose an alternative solution below.

Answer to

@jcwenger is very useful, but I was wondering if something had changed in the last 3 years. I ran into the same problem, and maybe since ORMLite has matured, is it more useful to use ORMLite?

My colleague next to me really, really wants to use ORMLite, since he does not want to write the mapping itself. I am aware of the existence of atleap and Android-OrmLiteContentProvider projects. They only provide an activity cursor, and my colleague wants to have lists of models or one model. Could this be achieved?

My colleague suggests writing my own implementation of Cursor, SyncAdapter? and Contentprovider (needs to be done independently) for working with models. However, can the same functionality be implemented using lists, etc.? Passing events to activity for content servers, etc.

Is it viable?

Edit Most likely, we will use the content content privately. We do not need to expose these content providers. However, the benefits provided by content providers are great. How else can I notify my GUI to update when data changes?

I also need to display data from several tables (joins and other data not contained in one table) in one action and upload images, etc.

+7
android android-contentprovider ormlite
source share
1 answer

Since I could not find the correct answer, here is how I decided to try this after a while:

public class CardProvider extends ContentProvider { private InternalDatabase dbhelper; private RuntimeExceptionDao<Card, UUID> cardDao; /** * Content authority for this provider. */ private static final String AUTHORITY = CardUris.CONTENT_AUTHORITY; // The constants below represent individual URI routes, as IDs. Every URI pattern recognized by // this ContentProvider is defined using sUriMatcher.addURI(), and associated with one of these // IDs. // // When a incoming URI is run through sUriMatcher, it will be tested against the defined // URI patterns, and the corresponding route ID will be returned. /** * URI ID for route: /cards */ public static final int ROUTE_CARDS = 1; /** * URI ID for route: /cards/{ID} */ public static final int ROUTE_CARDS_ID = 2; /** * UriMatcher, used to decode incoming URIs. */ private static final UriMatcher sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH); static { sUriMatcher.addURI(AUTHORITY, "cards", ROUTE_CARDS); sUriMatcher.addURI(AUTHORITY, "cards/*", ROUTE_CARDS_ID); } @Override public int delete(Uri arg0, String arg1, String[] arg2) { // TODO Auto-generated method stub return 0; } @Override public String getType(Uri uri) { final int match = sUriMatcher.match(uri); switch (match) { case ROUTE_CARDS: return CardUris.CONTENT_CARDS; case ROUTE_CARDS_ID: return CardUris.CONTENT_ITEM_CARD; default: throw new UnsupportedOperationException("Unknown uri: " + uri); } } @Override public Uri insert(Uri arg0, ContentValues arg1) { // TODO Auto-generated method stub return null; } @Override public boolean onCreate() { dbhelper = OpenHelperManager.getHelper(getContext(), InternalDatabase.class); cardDao = dbhelper.getRuntimeExceptionDao(Card.class); return true; } @Override public Cursor query(Uri uri, String[] arg1, String arg2, String[] arg3, String arg4) { int uriMatch = sUriMatcher.match(uri); switch (uriMatch) { case ROUTE_CARDS_ID: /*String id = uri.getLastPathSegment(); Card card = null; try { card = cardDao.queryBuilder().where().eq(Entry.ID_FIELD_NAME, id).queryForFirst(); } catch (SQLException e) { e.printStackTrace(); }*/ //return null; case ROUTE_CARDS: // Return all known entries. // Note: Notification URI must be manually set here for loaders to correctly // register ContentObservers. // build your query QueryBuilder<Card, UUID> qb = cardDao.queryBuilder(); // when you are done, prepare your query and build an iterator CloseableIterator<Card> iterator = null; Cursor cursor = null; try { //qb.query(); iterator = cardDao.iterator(qb.where().eq("relevant", 1).and().eq("removed", false).prepare()); // get the raw results which can be cast under Android AndroidDatabaseResults results = (AndroidDatabaseResults)iterator.getRawResults(); cursor = results.getRawCursor(); } catch (SQLException e) { e.printStackTrace(); } finally { //iterator.closeQuietly(); } cursor.setNotificationUri(this.getContext().getContentResolver(), uri); return cursor; default: throw new UnsupportedOperationException("Unknown uri: " + uri); } } @Override public int update(Uri arg0, ContentValues arg1, String arg2, String[] arg3) { // TODO Auto-generated method stub return 0; } } 

You could probably point out the methods for inserting, updating, and deleting, but Tao does it too and I'm using it.

+3
source share

All Articles