Room-字符串类型转换器的数组

时间:2018-07-25 13:54:17

标签: android android-room android-architecture-components

是否有人将数组类型的类型转换器作为逗号分隔的字符串列表存储在Room数据库中?

寻求帮助

2 个答案:

答案 0 :(得分:3)

public class Converters {
    @TypeConverter
    public String get_string(List<String> str) {
        if (str == null)
            return null;
        StringBuilder pictures = new StringBuilder();
        for (String s : str) pictures.append(s).append(",");
        return pictures.toString();
    }

    @TypeConverter
    public List<String> set_string(String str) {
        if (str == null)
            return null;
        return new ArrayList<>(Arrays.asList(str.split(",")));
    }
}

答案 1 :(得分:1)

类似的事情应该起作用

 public static class Converter {

    @TypeConverter
    public String fromArray(ArrayList<String> strings) {
        String string = "";
        for(String s : strings) string += (s + ",");

        return string;
    }

    @TypeConverter
    public ArrayList<String> toArray(String concatenatedStrings) {
        ArrayList<String> myStrings = new ArrayList<>();

        for(String s : concatenatedStrings.split(",") myStrings.append(s);

        return myStrings;
    }
}