Arp*_*pit 8 android bulkinsert android-contentprovider
在将所有批量插入完成到数据库之后,我只想要一个通知.请提供一个使用bulkInsert()函数的示例.我在互联网上找不到合适的例子.请帮忙!!!!
War*_*ock 38
这是使用ContentProvider的bulkInsert.
public int bulkInsert(Uri uri, ContentValues[] values){
int numInserted = 0;
String table;
int uriType = sURIMatcher.match(uri);
switch (uriType) {
case PEOPLE:
table = TABLE_PEOPLE;
break;
}
SQLiteDatabase sqlDB = database.getWritableDatabase();
sqlDB.beginTransaction();
try {
for (ContentValues cv : values) {
long newID = sqlDB.insertOrThrow(table, null, cv);
if (newID <= 0) {
throw new SQLException("Failed to insert row into " + uri);
}
}
sqlDB.setTransactionSuccessful();
getContext().getContentResolver().notifyChange(uri, null);
numInserted = values.length;
} finally {
sqlDB.endTransaction();
}
return numInserted;
}
Run Code Online (Sandbox Code Playgroud)
当ContentValues [] values数组中有更多ContentValues时,只调用一次.
我一直在寻找一个教程来在活动端和内容提供者端实现这一点。我使用了上面的“术士”答案,它在内容提供商方面效果很好。我使用这篇文章中的答案在活动端准备了 ContentValues 数组。我还修改了我的 ContentValues 以从一串逗号分隔值(或新行、句点、分号)中接收。看起来像这样:
ContentValues[] bulkToInsert;
List<ContentValues>mValueList = new ArrayList<ContentValues>();
String regexp = "[,;.\\n]+"; // delimiters without space or tab
//String regexp = "[\\s,;.\\n\\t]+"; // delimiters with space and tab
List<String> splitStrings = Arrays.asList(stringToSplit.split(regexp));
for (String temp : splitStrings) {
Log.d("current student name being put: ", temp);
ContentValues mNewValues = new ContentValues();
mNewValues.put(Contract.KEY_STUDENT_NAME, temp );
mNewValues.put(Contract.KEY_GROUP_ID, group_id);
mValueList.add(mNewValues);
}
bulkToInsert = new ContentValues[mValueList.size()];
mValueList.toArray(bulkToInsert);
getActivity().getContentResolver().bulkInsert(Contract.STUDENTS_CONTENT_URI, bulkToInsert);
Run Code Online (Sandbox Code Playgroud)
我找不到更简洁的方法将描述的拆分字符串直接附加到用于bulkInsert 的 ContentValues 数组。但是这个功能直到我找到它为止。
| 归档时间: |
|
| 查看次数: |
11906 次 |
| 最近记录: |