本文介绍了在ORMLite坚持的HashMap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

林在我的Andr​​oid应用程序使用ORMLite。我需要坚持这一类,它有一个HashMap。什么是坚持它的好方法?它的我第一次尝试坚持一个HashMap,也是第一次与ORMLite所以任何建议将不胜AP preciated!

*的修改的* 如果说有什么区别,运动类只是一个字符串(也可以作为ID在数据库中),以及一组类有一个int ID(这也是ID在数据库中),INT重量和int代表。

  @DatabaseTable
公共类锻炼{

    @DatabaseField(generatedId =真)
    INT ID;

    @DatabaseField(canBeNull = FALSE)
    创建日期;

    / *
     * HashMap中需要以某种方式坚持
     * /
    HashMap的<运动,ArrayList的<集>> workoutMap;

    公开训练(){
    }
    公开训练(HashMap的<运动,ArrayList的<集>> workoutMap,创建日期){
        this.workoutMap = workoutMap;
        this.created =创造的;
    }

    公共无效addExercise(练习即ArrayList中<集> SETLIST){
        workoutMap.put(即SETLIST);
    }
    ...
}
 

解决方案

哇。坚持一个的HashMap ,其值是一个列表 取值。林pressive。

因此​​,在 ORMLite 你能坚持任何序列化字段。下面是关于类型以及如何配置它的文档:

所以,你的领域看起来是这样的:

  @DatabaseField(的dataType = DataType.SERIALIZABLE)
地图<运动,名单,其中,集>> workoutMap;
 

请注意:的,如果地图是在所有大的,那么这将极有可能不会很高性能。此外,你的练习类(和列表设置班)需要实施序列化

如果你需要搜索这个地图,你可以考虑在设置另一个表在这种情况下,你可能想看看如何ORMLite仍然存在储存的值异物

Im using ORMLite in my Android app. I need to persist this class, which has a HashMap. What is a good way of persisting it? Its my first time trying to persist a HashMap, also first time with ORMLite so any advice would be greatly appreciated!

*Edit*If that makes any difference, the Exercise class is simply a String (that also works as id in the database), and the Set class has an int id (which is also id in database), int weight and int reps.

@DatabaseTable
public class Workout {

    @DatabaseField(generatedId = true)
    int id;

    @DatabaseField(canBeNull = false)
    Date created;

    /*
     * The hashmap needs to be persisted somehow
     */
    HashMap<Exercise, ArrayList<Set>> workoutMap;

    public Workout() {          
    }
    public Workout(HashMap<Exercise, ArrayList<Set>> workoutMap, Date created){
        this.workoutMap = workoutMap;
        this.created = created;
    }

    public void addExercise(Exercise e, ArrayList<Set> setList) {
        workoutMap.put(e, setList);
    }
    ... 
}
解决方案

Wow. Persisting a HashMap whose value is a List of Sets. Impressive.

So in ORMLite you can persist any Serializable field. Here's the documentation about the type and how you have to configure it:

So your field would look something like:

@DatabaseField(dataType = DataType.SERIALIZABLE)
Map<Exercise, List<Set>> workoutMap;

Please note that if the map is at all large then this will most likely not be very performant. Also, your Exercise class (and the List and Set classes) need to implement Serializable.

If you need to search this map, you might consider storing the values in the Set in another table in which case you might want to take a look at how ORMLite persists "foreign objects".

这篇关于在ORMLite坚持的HashMap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 09:24