Có thể thực hiện giao dịch dựa trên nhiều bảng chèn khá sạch kể từ Android 2.1 bằng cách sử dụng ContentProviderOperation, như đã đề cập bởi kaciula.
Khi bạn xây dựng đối tượng ContentProviderOperation, bạn có thể gọi .withValueBackReference (fieldName, refNr). Khi hoạt động được áp dụng bằng hàm applyBatch, kết quả là đối tượng ContentValues được cung cấp với lệnh insert() sẽ có một số nguyên được tiêm. Số nguyên sẽ được keyed với fieldName String, và giá trị của nó được lấy từ ContentProviderResult của một ContentProviderOperation được áp dụng trước đó, được lập chỉ mục bởi refNr.
Vui lòng tham khảo mẫu mã bên dưới. Trong mẫu, một hàng được chèn vào bảng 1 và ID kết quả (trong trường hợp này là "1") sau đó được sử dụng như một giá trị khi chèn hàng trong bảng 2. Để ngắn gọn, trình ContentProvider không được kết nối với cơ sở dữ liệu. Trong ContentProvider, có các bản in nơi nó sẽ phù hợp để thêm xử lý giao dịch.
public class BatchTestActivity extends Activity {
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayList<ContentProviderOperation> list = new
ArrayList<ContentProviderOperation>();
list.add(ContentProviderOperation.
newInsert(BatchContentProvider.FIRST_URI).build());
ContentValues cv = new ContentValues();
cv.put("name", "second_name");
cv.put("refId", 23);
// In this example, "refId" in the contentValues will be overwritten by
// the result from the first insert operation, indexed by 0
list.add(ContentProviderOperation.
newInsert(BatchContentProvider.SECOND_URI).
withValues(cv).withValueBackReference("refId", 0).build());
try {
getContentResolver().applyBatch(
BatchContentProvider.AUTHORITY, list);
} catch (RemoteException e) {
e.printStackTrace();
} catch (OperationApplicationException e) {
e.printStackTrace();
}
}
}
public class BatchContentProvider extends ContentProvider {
private static final String SCHEME = "content://";
public static final String AUTHORITY = "com.test.batch";
public static final Uri FIRST_URI =
Uri.parse(SCHEME + AUTHORITY + "/" + "table1");
public static final Uri SECOND_URI =
Uri.parse(SCHEME + AUTHORITY + "/" + "table2");
public ContentProviderResult[] applyBatch(
ArrayList<ContentProviderOperation> operations)
throws OperationApplicationException {
System.out.println("starting transaction");
ContentProviderResult[] result;
try {
result = super.applyBatch(operations);
} catch (OperationApplicationException e) {
System.out.println("aborting transaction");
throw e;
}
System.out.println("ending transaction");
return result;
}
public Uri insert(Uri uri, ContentValues values) {
// this printout will have a proper value when
// the second operation is applied
System.out.println("" + values);
return ContentUris.withAppendedId(uri, 1);
}
// other overrides omitted for brevity
}
Nguồn
2011-11-17 13:45:11
thấy điều này: http://stackoverflow.com/questions/4655291/semantics-of-withvaluebackreference –
Bạn đã bao giờ tìm thấy một giải pháp cho điều này? Tôi không thể tìm thấy một giải pháp hoạt động – jamesc