gpt4 book ai didi

android - 插入子实体后,GreenDAO 不会更新实体的子树

转载 作者:行者123 更新时间:2023-11-29 15:43:31 25 4
gpt4 key购买 nike

我有两个 GreenDAO 实体,“Card”与“Lesson”有 n-1 关系。

public class Card {

private Long id;
private String SourceText;
private String TargetText;
private byte[] Image;
private Long Point;
private Long lessonID;
}

public class Lesson {

private Long id;
private String LessonName;
private String ShortDes;
private String LongDes;
private byte[] Picture;
private java.util.Date CreatedDate;
private String CreatedBy;
private Long sourceLangID;
private Long targetLangID;

/** Used to resolve relations */
private transient DaoSession daoSession;

/** Used for active entity operations. */
private transient LessonDao myDao;


private List<Card> cards;
}

在 Activity A1 - 查看类(class) L1 中,我使用了 startActivityForResult(A2),转到 A2,并将一些卡片插入到 L1 的卡片列表中。记录被插入到数据库中,但是当我在 A2 中完成()并返回到 A1 时,在 onResult 事件中:

void loadCards() {
daoSession = null;
daoSession = ((FlashcardApplication) getApplicationContext()).daoSession;
lessonDao = daoSession.getLessonDao();
currentLesson = null;
currentLesson = lessonDao.loadByRowId(id);
cards = currentLesson.getCards();
lblLessonName.setText("Cards of "+currentLesson.getLessonName());
daoSession = ((FlashcardApplication) getApplicationContext()).daoSession;
// cards = cardDao.queryDeep("lessonID = " + currentLesson.getId(), null);
CardListAdapter adapter = new CardListAdapter(getApplicationContext(), R.layout.lv_item_card_of_lesson, cards);
lvCards.setAdapter(adapter);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == 1){
if(resultCode == CardCreateActivity.SUCCESS){
loadCards();
Toast.makeText(getApplicationContext(), "This set have " + cards.size() +" cards",Toast.LENGTH_SHORT).show();
}
return;
}
Toast.makeText(getApplicationContext(),"Unknowed intent when back to LocalLessonCardManage",Toast.LENGTH_SHORT);
}

卡片列表未更新。还是和插入前一样。当我重新启动并访问 Activity A1 时,显示了新记录。我认为 GreenDAO 正在保持类(class)对象 L1 的状态。我知道 lessonDao.refresh(l1) 只更新 L1 而不是它的子树。有没有办法刷新卡片列表?我想继续使用 startActivityForResult(),可能是一种肮脏的方式 startActivity()A2A1 可以,但我不喜欢。

最佳答案

出于性能原因,关系被缓存并且必须在进行修改时手动更新。

试试这个

currentLesson.resetCards();
cards = currentLesson.getCards();

Here你有更多关于这个的信息

关于android - 插入子实体后,GreenDAO 不会更新实体的子树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36612195/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com