我在更新 RecycleView 中的项目时遇到问题。
当我尝试编辑梦想对象并保存它时,它会在数据库中更新,但不会在梦想
列表中更新,我不知道为什么。
此方法创建并更新梦想对象。
@OnClick({R.id.addTagDream, R.id.saveDreamButton})
public void onViewClicked(View view) {
switch (view.getId()) {
case R.id.addTagDream:
Intent intent = new Intent(AddEditDreamActivity.this, SelectDreamActivity.class);
startActivityForResult(intent, SELECTED_DREAM);
break;
case R.id.saveDreamButton:
boolean hasError = false;
...
if (!hasError) {
// check if exit dream about id
try {
myDreamList = myDreamDao.queryForEq(MyDream.Columns.MY_DREAM_ID, dreamId);
} catch (SQLException e) {
e.printStackTrace();
}
chcek if dream exist
if (myDreamList != null && !myDreamList.isEmpty()) {
// update dream
UpdateBuilder<MyDream, ?> updateMyDream = myDreamDao.updateBuilder();
// condition update dream about dream id
try {
updateMyDream.where().eq(MyDream.Columns.MY_DREAM_ID, dreamId);
} catch (SQLException e) {
e.printStackTrace();
}
// update column title dream
try {
updateMyDream.updateColumnValue(MyDream.Columns.TITLE_DREAM, dreamTitle);
} catch (SQLException e) {
e.printStackTrace();
}
// update dream description
try {
updateMyDream.updateColumnValue(MyDream.Columns.DESCRIPTION, dreamDescription);
} catch (SQLException e) {
e.printStackTrace();
}
// update dream symbol
try {
updateMyDream.updateColumnValue(MyDream.Columns.SYMBOL_DREAM, selectSymbolDream);
} catch (SQLException e) {
e.printStackTrace();
}
try {
updateMyDream.update();
} catch (SQLException e) {
e.printStackTrace();
}
finish();
}
}
break;
}
}
最佳答案
每当您在列表中添加值时,请使用adapter.notifyDataSetChanged()
或notifyItemInserted()
希望这对您有帮助
关于java - 更新 RecyclerView 中的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46915486/