Ori*_*ion 7 android ormlite android-contentprovider
我在Google IO演示文稿中看到了模式C,我非常渴望实现这种模式.但是,我确实喜欢ORMLite库,并且想在我的应用程序中使用这个库.
当我说谷歌IO演示时,我的意思是这个:https://www.youtube.com/watch? v = xHXn3Kg2IQE由Virgil Dobjanschi提供.
现在我一直在寻找一个实现,它向我展示了如何将ORMLite与Contentproviders结合使用.
现在我的问题是ORMLite DAO与Contentprovider冲突.他们基本上做同样的事情并且很难相互融合.(使用Ormlite与Android的内容提供商合作,其他人讨论此事并同意此声明.)
一些库已经将ORMLite实现到contentprovider API模式中,例如:https://github.com/blandware/android-atleap
然而,在水下他们仍然将模型还原为ContentValues(简单类型).
Android - 使用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 arg2, String[] arg3) {
// TODO Auto-generated method stub
return 0;
}
}
Run Code Online (Sandbox Code Playgroud)
您可能可以为插入、更新和删除方法指定目的,但 dao 也可以执行此操作,并且这就是我正在使用的。
| 归档时间: |
|
| 查看次数: |
3369 次 |
| 最近记录: |