主从使用ContentResolver.applyBatch()? [英] Master-detail Using ContentResolver.applyBatch()?

查看:1479
本文介绍了主从使用ContentResolver.applyBatch()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在想,如果可能的主页和详细记录保存到使用android.content.ContentResolver.applyBatch()方法相同的操作内容提供商,其中的供应商参数随后ContentProviderOperation项目依赖于previous项目的结果。

I was wondering if its possible to save master and detail records to a content provider using the android.content.ContentResolver.applyBatch() method in the same operation where subsequent ContentProviderOperation items in the providers parameter depend on the result of previous items.

我遇到的问题是,实际的开放的我们不知道当时的ContentProviderOperation.newInsert(URI)方法被调用和URI是不可变的。

The problem I'm having is that the actual Uri isn't known at the time that the ContentProviderOperation.newInsert(Uri) method is called and the Uri is immutable.

我想出了如下所示:

What I have come up with is shown below:

主网址:内容://com.foobar.masterdetail/master
详细网址:内容://com.foobar.masterdetail/master/#/detail

Master Uri: content://com.foobar.masterdetail/master
Detail Uri: content://com.foobar.masterdetail/master/#/detail

ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>();
operations.add(ContentProviderOperation.newInsert(intent.getData())
    .withValue(Master.NAME, "")
    .withValue(Master.VALUE, "")
    .build());
operations.add(ContentProviderOperation.newInsert(intent.getData()
        .buildUpon()
        .appendPath("#") /* ACTUAL VALUE NOT KNOWN UNTIL MASTER ROW IS SAVED */
        .appendPath("detail")
        .build())
    .withValue(Detail.MASTER_ID, /* WHAT GOES HERE? */)
    .withValue(Detail.NAME, "")
    .withValue(Detail.VALUE, "")
    .build());
ContentProviderResult[] results = this.getContentResolver().applyBatch(MasterDetail.AUTHORITY, operations);
for (ContentProviderResult result : results) {
    Uri test = result.uri;
}

在我的内容提供商,我重写了applyBatch()方法,以包装在一个事务中的操作。

In my content provider, I am overriding the applyBatch() method in order to wrap the operation in a transaction.

这是可能的,或有没有更好的方式来做到这一点?

Is this possible or is there a better way to do this?

感谢。

推荐答案

从操作阵列是由它在数组中的索引标识项目产生的每个结果。 随后的操作可以通过withValueBackReference()方法引用这些结果。

Each result produced from an item in the operations array is identified by its index in the array. Subsequent operations may reference those results via the withValueBackReference() method.

.withValue(Detail.MASTER_ID, /* WHAT GOES HERE? */)

变为

.withValueBackReference(Detail.MASTER_ID, 0)

这种用法的一个完整的例子可以在<一个被发现href="http://developer.android.com/resources/samples/ContactManager/src/com/example/android/contactmanager/ContactAdder.html">sample的ContactManager 的。 的0是从其中获得的值ContentProviderOperation的索引

A complete example of this usage can be found in sample ContactManager. The 0 is the index of the ContentProviderOperation from which the value is obtained.

这篇关于主从使用ContentResolver.applyBatch()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆