添加,删除ForeignCollection中的对象

May*_*Man 4 android ormlite foreign-collection

我有一个实体,其中包含一个带有getter和setter的ForeignCollection

public class TextQuestion{

    @ForeignCollectionField
    private ForeignCollection<TextAnswer> answers;
    ....
Run Code Online (Sandbox Code Playgroud)

我有一个封装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);
    }
Run Code Online (Sandbox Code Playgroud)

我的问题是如何通过Wrapper服务类添加,删除此集合中的项目?上面是我的努力,但我刚刚完全丢失,将集合转换为列表等.问题是当我想添加一个问题的答案ForeignCollection如果Answers集合不是null但是我想构建提问,在坚持之前添加答案.从前一个问题我明白,如果尚未从数据库中检索父级,我需要调用getEmptyForeignCollection.必须有一个简单的方法来做到这一点?任何工作类似的例子都会很好

Gra*_*ray 6

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

我觉得你很亲密 你应该能够做到:

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

answers也可以是一个,Collection<TextAnswer>但它不是一个List.