【发布时间】:2020-02-04 11:06:54
【问题描述】:
我有一个使用 MVVM 构建的应用程序。我让应用程序成功插入 SQLite 数据库。我现在尝试在插入完成后获取数据库中的 rowId,以便可以对该行进行后续活动更新。
我完全理解我们在 DAO 中将 Void 更改为 Long 的部分,但我不知道如何在活动级别实际访问该 long 并将其保存到可以使用 Intent.PUTEXTRA 从活动发送到活动的变量中.下面的代码示例。
表/实体类:
@Entity(tableName = "CbtTable")
public class CbtTable {
@PrimaryKey(autoGenerate = true)
private long cbtId;
存储库类:
public class CbtRepository {
private CbtDao cbtDao;
private LiveData<List<CbtTable>> cbtData;
private FeelingFitDatabase mFeelingFitDatabase;
public CbtRepository(Application application) {
mFeelingFitDatabase = FeelingFitDatabase.getInstance(application);
cbtDao = mFeelingFitDatabase.getCbtDao();
cbtData = cbtDao.getCbtData();
}
public void insertCbt(CbtTable cbtTable){
new InsertCbtAsyncTask(cbtDao).execute(cbtTable);
}
道
@Dao
public interface CbtDao {
@Insert(onConflict = REPLACE)
public long insertAutoThought(CbtTable cbtTable);
@Query("UPDATE CbtTable SET automaticThought = :automaticThought WHERE cbtId = :cbtId")
void updateAutoThought(long cbtId, String automaticThought);
@Query ("UPDATE CbtTable SET twistedThinking = :twistedThinking WHERE cbtId = :cbtId")
void updateTwistedThinking(long cbtId, int twistedThinking);
ViewModel 类:
public class CbtViewModel extends AndroidViewModel {
private CbtRepository cbtRepository;
private LiveData<List<CbtTable>> cbtData;
public CbtViewModel(@NonNull Application application) {
super(application);
cbtRepository = new CbtRepository(application);
cbtData = cbtRepository.getCbtData();
}
public void insert(CbtTable cbtTable){
cbtRepository.insertCbt(cbtTable);
}
public void updateTwistedThinking(CbtTable cbtTable){
cbtRepository.updateTwistedThinking(cbtTable);
}
异步任务类:
public class InsertCbtAsyncTask extends AsyncTask<CbtTable, Void, Void> {
private CbtDao mCbtDao;
public InsertCbtAsyncTask(CbtDao cbtDao){
mCbtDao = cbtDao;
}
@Override
protected Void doInBackground(CbtTable... cbtTables) {
mCbtDao.insertAutoThought(cbtTables[0]);
return null;
}
活动方式:
private void saveAutomaticThought(){
CbtTable cbtTable = new CbtTable(userId, automaticThoughtString, twistedThinking, challengeThoughtString, rationalThoughtString, postedWorkout);
cbtViewModel.insert(cbtTable);
long cbtId = cbtTable.getCbtId();
// Sends the user to the WorkoutAutomaticThoughtActivity page
Intent intent = new Intent(this, WorkoutTwistedThinkingActivity.class);
intent.putExtra("cbtId", cbtId);
使用 Activity 中的上述代码,cbtId 存储为 0,因此无法正常工作。这是我正在开发的免费心理健康应用程序,旨在帮助人们,因此我越早解决这个问题,我就能越早推出该应用程序。我将非常感谢调试此问题并使其正常工作的任何帮助。
【问题讨论】:
-
我真的建议你停止使用 AsynTask。如果你使用它,你可以让那个类接收一个回调函数并在 onPostExecute 中调用它。目前,您忽略了 mCbtDao.insertAutoThought 的结果。只需将其存储在 AsynTask 类中的变量中,稍后在 onPostExecute 方法中使用它。
-
你能帮我写代码吗?我是新手,不知道如何构造onPostExecute方法来获取我需要的信息并在activity/fragment中访问。
标签: java android sqlite mvvm android-room