5

有没有一种简单的方法可以从 java 转换Map<String,Object>android.content.ContentValues.

android.content.ContentValues在android数据库编程中用于将数据库行的数据保存为名称-值对。

背景:

我尝试将现有 android 应用程序的业务逻辑移植到 j2se-swing-app

我的目标是让 android 特定代码和 android 独立代码进入一个单独的库,供 android 和 swing-gui 使用。

目前我正在分析

  • 如果我必须使用大量冗余代码实现一组完整的独立存储库实现
  • 或者如果有可以在(j2se-swing-和android-)存储库-实现中使用的通用代码。

Repository-Database-Code 依赖于使用android.content.ContentValues.

我认为我的 android 独立版本可以使用HashMap<String,Object>inseadContentValues并创建代码在两者之间进行转换。

android依赖版本看起来像这样

    // android dependant code in android-app
    class AndroidTimeSliceCategoryRepsitory implements ICategoryRepsitory {

        public long createTimeSliceCategory(final TimeSliceCategory category) {

            // get values from android independant layer
            Map<String, Object> columsToBeInserted = TimeSliceCategorySql.asHashMap(category);

// >>>> this is where i am stuck
            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;
        }
    }

这是android独立部分:

    // android independant code in common jar
    class TimeSliceCategorySql {....

        /** converts {@link ....model.TimeSliceCategory}  to {@link java.util.HashMap} */
        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());

            // ... more values

            return values;
        }
    }

目前我被困在这里:

    public class AndroidDatabaseUtil {
        /** converts from android independeant {@link java.util.Map} to android dependent {@link android.content.ContentValues} */
        public static ContentValues toContentValues(Map<String, Object> src) {
            ContentValues result = new ContentValues();

            for (String name : src.keySet()) {

// this is not possible because ContentValues does not define put(String name, Object value)
                result.put(name, src.get(name));

// using this would loose ContentValues.getAsLong() throw exception.
// result.put(name, src.get(name).toString());

            }
            src.entrySet()
            return result;
        }
    }
4

3 回答 3

6

如何编组/解组 ContentValues 以将泛型类型插入 ContentProvider?

Location loc = mLocationClient.getLastLocation();
HashMap hm = new HashMap();
hm.put("LOCATIONS", loc);
Parcel myParcel = Parcel.obtain();    
myParcel.writeMap(hm);
myParcel.setDataPosition(0);

ContentValues values = ContentValues.CREATOR.createFromParcel(myParcel);

getContentResolver().insert(MyDumbUri, values);

接着

@Override
public Uri insert( final Uri uri, final ContentValues oldvalues ){
SQLiteDatabase db = GAELdatabase.getWritableDatabase();

Uri result = null;
Location loc = (Location)oldvalues.get("LOCATIONS");

ContentValues values = new ContentValues();

values.put("ALTITUDE", loc.getAltitude());//meters above sea level
values.put("LATITUDE", loc.getLatitude());
values.put("LONGITUDE", loc.getLongitude());

long rowID = db.insert( "MyTABLE_NAME", "", values);
db.close();
return result;
}
于 2014-10-11T01:14:30.430 回答
2

看起来您在 Map 中输入的值是字符串。

因此,您可以将 Map 的定义更改为

final Map<String, String> values = new HashMap<String, String>();

这将允许您将值放入ContentValues实例中:

result.put(name, src.get(name));
于 2014-08-02T11:17:47.730 回答
2

您可以使用语句中的instanceof关键字对其进行过滤。if

例子:

if( src.get(name) instanceof String){
  // convert to string
}

您还可以使用getClass对象中的方法:

对象 ob = src.get(name);

  if(ob instanceof ob.getClass()){
   // convert to type
  }
于 2014-08-02T11:30:28.387 回答