cur*_*zen 39 android android-contentprovider
我无法弄清楚withValueBackReference的确切语义.
我已经使用这种方法读取了示例代码(例如添加新联系人的代码),提供了backReference值为0.这是什么意思?
文件说:
后引用的列值优先于withValues(ContentValues)中指定的值
Mer*_*lin 162
此问题涉及内容提供商的批处理操作.该示例从此相关问题进行了修改.
创建一批操作时,首先要创建要执行的操作列表:
ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>();
Run Code Online (Sandbox Code Playgroud)
然后使用applyBatch方法将它们应用于内容提供程序.
ContentProviderResult[] results = this.getContentResolver().applyBatch(FooBar.AUTHORITY, operations);
Run Code Online (Sandbox Code Playgroud)
这是基本概念所以让我们应用它.假设我们有一个内容提供程序来处理用于Foo记录的uris和一些名为Bar的子记录.
内容://com.stackoverflow.foobar/foo
内容://com.stackoverflow.foobar/foo/#/bar
现在我们只需插入2个新富recordscalled"富A"和"富B",这里的例子.
ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>();
//add a new ContentProviderOperation - inserting a FOO record with a name and a decscription
operations.add(ContentProviderOperation.newInsert(intent.getData())
.withValue(FOO.NAME, "Foo A")
.withValue(FOO.DESCRIPTION, "A foo of impeccable nature")
.build());
//let's add another
operations.add(ContentProviderOperation.newInsert(intent.getData())
.withValue(FOO.NAME, "Foo B")
.withValue(FOO.DESCRIPTION, "A foo of despicable nature")
.build());
ContentProviderResult[] results = this.getContentResolver().applyBatch(FooBar.AUTHORITY, operations);
Run Code Online (Sandbox Code Playgroud)
这里没有什么特别,我们增加2个ContentProviderOperation项目我们的列表,然后将列表中我们内容提供商.结果数组填充了我们刚刚插入的新记录的id.
所以说我们想做类似的事情,但我们也希望在一次批处理操作中将一些子记录添加到我们的内容提供者中.我们希望将子记录附加到我们刚刚创建的Foo记录中.问题是我们不知道父Foo记录的id,因为批处理尚未运行.这是withValueBackReference帮助我们的地方.我们来看一个例子:
ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>();
//add a new ContentProviderOperation - inserting a Foo record with a name and a decscription
operations.add(ContentProviderOperation.newInsert(intent.getData())
.withValue(FOO.NAME, "Foo A")
.withValue(FOO.DESCRIPTION, "Foo of impeccable nature")
.build());
//let's add another
operations.add(ContentProviderOperation.newInsert(intent.getData())
.withValue(FOO.NAME, "Foo B")
.withValue(FOO.DESCRIPTION, "Foo of despicable nature")
.build());
//now add a Bar record called [Barbarella] and relate it to [Foo A]
operations.add(ContentProviderOperation.newInsert(intent.getData()
.buildUpon()
.appendPath("#") /* We don't know this yet */
.appendPath("bar")
.build())
.withValueBackReference (BAR.FOO_ID, 0) /* Index is 0 because Foo A is the first operation in the array*/
.withValue(BAR.NAME, "Barbarella")
.withValue(BAR.GENDER, "female")
.build());
//add a Bar record called [Barbarian] and relate it to [Foo B]
operations.add(ContentProviderOperation.newInsert(intent.getData()
.buildUpon()
.appendPath("#") /* We don't know this yet */
.appendPath("bar")
.build())
.withValueBackReference (BAR.FOO_ID, 1) /* Index of parent Foo B is 1*/
.withValue(BAR.NAME, "Barbarian")
.withValue(BAR.GENDER, "male")
.build());
ContentProviderResult[] results = this.getContentResolver().applyBatch(FooBar.AUTHORITY, operations);
Run Code Online (Sandbox Code Playgroud)
因此withValueBackReference()方法允许我们在知道要将它们关联到的父级的id之前插入相关记录.后引用索引只是将返回我们想要查找的id的操作的索引.根据您希望包含id的结果,可能更容易想到它.例如,results[1]它将包含"Foo B"的id,因此我们用来引用"Foo B"的索引为1.
| 归档时间: |
|
| 查看次数: |
9206 次 |
| 最近记录: |