SQLite MVVM在活动中返回和获取rowID(&A) [英] SQLite MVVM return & obtain rowId in Activity

查看:63
本文介绍了SQLite MVVM在活动中返回和获取rowID(&A)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用MVVM构建的应用程序。我让应用程序成功地插入到SQLite数据库中。我现在正在尝试在插入完成后获取数据库中的rowId,以便在后续活动中可以对该行进行更新。

我完全理解DAO中将void更改为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

@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);

使用活动中的上述代码,cbtId存储为0,因此无法工作。这是一款免费的心理健康应用程序,我正在开发它,试图帮助人们,所以我越早解决这个问题,我就能越早推出这款应用程序。我将非常感谢任何帮助来调试这个问题,并使其工作。

推荐答案

我不建议您使用异步任务。但是你可以做这样的事情。

使您的InsertCbtAsyncTask接受回调作为参数,并在它具有插入的ID后调用它:

public class InsertCbtAsyncTask extends AsyncTask<CbtTable, Void, Void> {
    private CbtDao mCbtDao;
    private long id;
    private DoWithId callback;

    public InsertCbtAsyncTask(CbtDao cbtDao, DoWithId callback){
        mCbtDao = cbtDao;
        this.callback = callback;
    }

    @Override
    protected Void doInBackground(CbtTable... cbtTables) {
        id = mCbtDao.insertAutoThought(cbtTables[0]);
        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        callback.doWithId(id);
    }

    interface DoWithId {
        public void doWithId(long id);
    }

}
然后,在调用该类时,您可以将回调作为参数传递。例如:

    DoWithId callback = new DoWithId() {
        @Override
        public void doWithId(long id) {
            launchIntentWithId(id)
        }
    };
    new InsertCbtAsyncTask(cbtDao, callback).execute(cbtTable);

launchIntentWithId()如下所示:

public launchIntentWithId(long cbtId) {
    Intent intent = new Intent(this, WorkoutTwistedThinkingActivity.class);
    intent.putExtra("cbtId", cbtId);
    startActivity(Intent);
}

此代码未经过测试,可能不完整,但它应该会让您有一个完整的概念。

这篇关于SQLite MVVM在活动中返回和获取rowID(&A)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆