【发布时间】:2016-04-15 00:36:11
【问题描述】:
我有两个 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;
}
在活动 A1 - 查看课程 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);
}
卡片列表未更新。还是和插入前一样。当我重新启动并访问活动 A1 时,会显示新记录。我认为 GreenDAO 正在保持课程对象L1 的状态。我知道 lessonDao.refresh(l1) 仅更新 L1 而不是它的子树。有没有办法刷新卡片列表?我想继续使用startActivityForResult(),可能用一种肮脏的方式startActivity() 从A2 转到A1 会起作用,但我不喜欢它。
【问题讨论】:
标签: android orm refresh updates greendao