我在Google IO演示文稿中看到了模式C,我非常渴望实现这种模式。但是,我确实喜欢ORMLite库,并且也想在我的应用程序中使用这个库。
当我说Google IO演示文稿时,我的意思是:https://www.youtube.com/watch?v=xHXn3Kg2IQE来自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更新?
我还必须在一个活动中显示来自多个表(连接和其他数据,不包含在同一个表中)的数据并下载图像等。
答案 0 :(得分:3)
因为我无法找到合适的答案,所以这是我在尝试一段时间后解决的问题:
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;
}
}
你可能会给插入,更新和删除方法带来目的,但dao也是这样做的,也是我正在使用的。