添加,删除ForeignCollection中的对象

时间:2012-07-18 11:15:12

标签: android ormlite foreign-collection

我有一个包含带有getter& amp;的ForeignCollection的实体。集合的setter

public class TextQuestion{

    @ForeignCollectionField
    private ForeignCollection<TextAnswer> answers;
    ....

我有一个封装DAO

的Servicer包装器类
public class TextQuestionService
{

    private static TextQuestionService instance;
    private static Dao<TextQuestion, Integer> textQuestionDAO;
    private static DatabaseHelper dbHelper = new DatabaseHelper();


    public void addAnswer( TextQuestion textQuestion, TextAnswer answer )
    {
        List<Answer> answers = (List)textQuestion.getAnswers();
        if( answers == null )
        {
            try
            {
                answers = (List)textQuestionDAO.getEmptyForeignCollection("answers");
            } catch (SQLException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        answers.add(answer);
    }

我的问题是 如何通过Wrapper服务类添加,删除此集合中的项目? 以上就是我的努力,但我完全迷失了,将收藏品列入名单等。 问题是,当我想在问题ForeignCollection中添加一个答案时,如果Answers Collection不是null,那么就没有问题,但是我想构建问题,在持久化之前添加Answers。从前一个问题我明白,如果尚未从数据库中检索父级,我需要调用getEmptyForeignCollection。必须有一个简单的方法来做到这一点?任何有效的类似例子都很好

1 个答案:

答案 0 :(得分:6)

  

问题是,当我想在问题中添加一个AnswerCollection时,如果Answers Collection不是null,那么就没有问题,但我想构建问题,

我认为你很亲密。你应该能够做到:

ForeignCollection<TextAnswer> answers = textQuestion.getAnswers();
if (answers == null) {
    answers = textQuestionDAO.getEmptyForeignCollection("answers");
}
answers.add(answer);

answers也可以是Collection<TextAnswer>,但不是List

相关问题