Is there an easy way to convert from java Map<String,Object>to android.content.ContentValues?
android.content.ContentValues used in Android database programming to store database row data as name-value pairs.
Background:
I am trying to migrate the business logic of an existing Android application to j2se-swing-app
I have a special code for Android and an android independent code, which is included in a separate lib that can be used by both android and swing-gui.
I am currently analyzing
- if I need to implement a complete separate set of repositories with lots of redundant code
- or if there is generic code that can be used in both (j2se-swing- and android-) Implementation repositories.
Repository-Database-Code uses the name-database pairs in which it is used android.content.ContentValues.
I thought that the Android version without an android could use HashMap<String,Object>insead of ContentValuesand create code to convert between them.
The version dependent on android will look like this:
class AndroidTimeSliceCategoryRepsitory implements ICategoryRepsitory {
public long createTimeSliceCategory(final TimeSliceCategory category) {
Map<String, Object> columsToBeInserted = TimeSliceCategorySql.asHashMap(category);
ContentValues androidColumsToBeInserted = DbUtils.asContentValues(columsToBeInserted);
final long newID = AndroidTimeSliceCategoryRepsitory.DB.getWritableDatabase()
.insert(TimeSliceCategorySql.TIME_SLICE_CATEGORY_TABLE, null, androidColumsToBeInserted);
category.setRowId((int) newID);
return newID;
}
}
This is an independent part of Android:
class TimeSliceCategorySql {....
public static Map<String, Object> asHashMap(final TimeSliceCategory category) {
final Map<String, Object> values = new HashMap<String, Object>();
values.put(TimeSliceCategorySql.COL_CATEGORY_NAME,
category.getCategoryName());
values.put(TimeSliceCategorySql.COL_DESCRIPTION,
category.getDescription());
return values;
}
}
I'm currently stuck here:
public class AndroidDatabaseUtil {
public static ContentValues toContentValues(Map<String, Object> src) {
ContentValues result = new ContentValues();
for (String name : src.keySet()) {
result.put(name, src.get(name));
}
src.entrySet()
return result;
}
}