Java Code Examples for com.raizlabs.android.dbflow.structure.ModelAdapter#bindToInsertValues()

The following examples show how to use com.raizlabs.android.dbflow.structure.ModelAdapter#bindToInsertValues() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ContentUtils.java    From Meteorite with Apache License 2.0 5 votes vote down vote up
/**
 * Inserts the model into the {@link android.content.ContentResolver}. Uses the insertUri to resolve
 * the reference and the model to convert its data into {@link android.content.ContentValues}
 *
 * @param contentResolver The content resolver to use (if different from {@link FlowManager#getContext()})
 * @param insertUri       A {@link android.net.Uri} from the {@link ContentProvider} class definition.
 * @param model           The model to insert.
 * @return The Uri of the inserted data.
 */
@SuppressWarnings("unchecked")
public static <TableClass> Uri insert(ContentResolver contentResolver, Uri insertUri, TableClass model) {
    ModelAdapter<TableClass> adapter = (ModelAdapter<TableClass>) FlowManager.getModelAdapter(model.getClass());

    ContentValues contentValues = new ContentValues();
    adapter.bindToInsertValues(contentValues, model);
    Uri uri = contentResolver.insert(insertUri, contentValues);
    adapter.updateAutoIncrement(model, Long.valueOf(uri.getPathSegments().get(uri.getPathSegments().size() - 1)));
    return uri;
}
 
Example 2
Source File: ContentUtils.java    From Meteorite with Apache License 2.0 5 votes vote down vote up
/**
 * Inserts the list of model into the {@link ContentResolver}. Binds all of the models to {@link ContentValues}
 * and runs the {@link ContentResolver#bulkInsert(Uri, ContentValues[])} method. Note: if any of these use
 * autoIncrementing primary keys the ROWID will not be properly updated from this method. If you care
 * use {@link #insert(ContentResolver, Uri, Object)} instead.
 *
 * @param contentResolver The content resolver to use (if different from {@link FlowManager#getContext()})
 * @param bulkInsertUri   The URI to bulk insert with
 * @param table           The table to insert into
 * @param models          The models to insert.
 * @return The count of the rows affected by the insert.
 */
public static <TableClass> int bulkInsert(ContentResolver contentResolver, Uri bulkInsertUri,
                                          Class<TableClass> table, List<TableClass> models) {
    ContentValues[] contentValues = new ContentValues[models == null ? 0 : models.size()];
    ModelAdapter<TableClass> adapter = FlowManager.getModelAdapter(table);

    if (models != null) {
        for (int i = 0; i < contentValues.length; i++) {
            contentValues[i] = new ContentValues();
            adapter.bindToInsertValues(contentValues[i], models.get(i));
        }
    }
    return contentResolver.bulkInsert(bulkInsertUri, contentValues);
}