当我说Google IO演示时,我的意思是这个:https://www.youtube.com/watch?v=xHXn3Kg2IQE by Virgil dobjanschi.
现在我一直在寻找一个实现,它向我展示了如何将Ormlite与Contentproviders结合使用.
现在我的问题是Ormlite DAO与Contentprovider冲突.他们基本上做同样的事情并且很难相互融合. (Using Ormlite in Conjunction with Android’s Content Provider其他人讨论此事并同意这项索赔.)
一些库已经将Ormlite实现到contentprovider API模式中,例如:https://github.com/blandware/android-atleap
然而,在水下他们仍然将模型还原为ContentValues(简单类型).
Android – Using Dao Pattern with contentProvider
这个问题与我的情况类似,但3年前,我建议在下面提出替代解决方案.
@ jcwenger的回答非常有用,但我想知道过去3年是否有任何改变.我面临同样的问题,也许现在因为Ormlite已经成熟,使用Ormlite更有意义吗?
我旁边的同事真的非常想使用Ormlite,因为他不想自己编写任何映射.我知道atleap和Android-OrmliteContentProvider项目的存在.这些只为活动提供了一个光标,我的同事希望拥有模型列表或单个模型.这可以实现吗?
我的同事建议编写我自己的Cursor,SyncAdapter实现?和Contentprovider(必须完成)必须使用模型.但是,使用列表等仍然可以实现相同的功能吗?将事件传递给contentobservers等活动?
这可行吗?
编辑
我们很可能会私下使用内容提供商.我们不需要公开这些内容提供者.然而,内容提供商提供的优势很大.当数据发生变化时,我怎样才能通知我的GUI更新?
我还必须在一个活动中显示来自多个表(连接和其他数据,不包含在同一个表中)的数据并下载图像等.
解决方法
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[] arg3) {
// Todo Auto-generated method stub
return 0;
}
}
你可以给插入,更新和删除方法的目的,但dao也这样做,是我正在使用的.