@TypeConverter从List <SomeObject>到Android Room中的String gson.toJson(List <SomeObject>)

时间:2019-10-13 17:32:43

标签: java android json

错误: java.lang.IllegalArgumentException:类android.widget.SeekBar声明了多个名为mMinHeight的JSON字段

我的Typeconverter

public class ObjectConverter {

    private static Gson gson= new Gson();

    @TypeConverter
    public static List<DynamicItem> fromString(String s){


        if (s == null) {
            return Collections.emptyList();
        }

        Type listType = new TypeToken<List<DynamicItem>>() {}.getType();

        return gson.fromJson(s, listType);

    }
    @TypeConverter
    public static String fromObject(List<DynamicItem> someObjects) {

        //Error in the following line
        return gson.toJson(someObjects,new TypeToken<List<DynamicItem>>() {}.getType());
    }
}

第一个实体:

@Entity(foreignKeys = @ForeignKey(entity = AllTypes.class,
        parentColumns = "rid",
        childColumns = "busId",
        onDelete = ForeignKey.CASCADE))
public class BusItem {

    @PrimaryKey(autoGenerate = true)
    @ColumnInfo
    private int busId;


    @ColumnInfo
    private List<DynamicItem> proxyObj;
    //Getters & Setters


}

第二实体

@Entity
public class AllTypes implements Serializable {

    @NonNull
    @PrimaryKey(autoGenerate = true)
    @ColumnInfo()
    private int rid;

    @ColumnInfo
    private String type;

    @ColumnInfo
    private String timeStamp;


    @Embedded
    private BusItem busItem;

    @Embedded
    private Simple simple;

    //Getters & Setters

}

添加数据

@Override
    protected Void doInBackground(Void... voids) {

                Calendar calendar = Calendar.getInstance();
                String time = new SimpleDateFormat("HH:mm:ss").format(calendar.getTime());

                String date = new SimpleDateFormat("dd-MM-yyyy").format(calendar.getTime());


                busItem.setProxyObj(busItemList);
                allTypes.setType("BUS_N");
                allTypes.setTimeStamp(date + time);
                allTypes.setBusItem(busItem);
                allTypes.setTag(stag);


                //adding to database
                DBClient.getInstance(getApplicationContext()).getNotesDB()
                        .notesDao()
                        .insertAllTypes(allTypes);

                return null;
            }

DynamicItem

public class DynamicItem {

CustomEditText customEditText;
ImageView imageUri;
AudioItem audioItem;
String header;
String htmlEditText;

//Getters and Setters
}

我正在尝试在数据库中保存dynamicItems列表。该关系是(AllTypes&BusItem)(AllTypes&Simple)之间的一对一关系。 Image For Schema Understanding

0 个答案:

没有答案