简体   繁体   English

Java Android MVVM-如何将存储库中的变量发送回活动?

[英]Java Android MVVM - how to send a variable from Repository back to activity?

I can get rowId value using an ASynctask in onPostExecute method ,I am trying to send the value back to the activity so it can be stored and used later so how to do that. 我可以在onPostExecute方法中使用ASynctask来获取rowId值 ,我正在尝试将该值发送回活动,以便以后可以存储和使用该值,从而实现该目的。

Activity : 活动内容:

Note note = new Note(userId, therapistId, automaticThoughtString,  distortions, challengeThoughtString, alternativeThoughtString, postedWorkout);
             noteViewModel.insert(note).observe(WorkoutAutomaticThoughtActivity.this, new Observer<Long>() {
                @Override
                public void onChanged(Long cbtId) {

                    sqCbtId = cbtId;
                    Log.d(TAG, "AutomaticThought" + sqCbtId);
                }
            });

Viewmodel : 视图模型:

 public LiveData<Long> insert (Note note) {
    return repository.insert(note);  
} 

Repository : 仓库:

    public MutableLiveData<Long> insert(Note note) {
    final MutableLiveData<Long> cbtId = new MutableLiveData<>();
    new InsertNoteAsyncTask(noteDao, cbtId).execute(note);
    return cbtId; 

Async : 异步:

 public InsertNoteAsyncTask(NoteDao noteDao, MutableLiveData<Long> cbtId) {
    this.noteDao = noteDao;
}

@Override
protected Void doInBackground(Note... notes) {
    sqCbtId = noteDao.insert(notes[0]);
    return null;
}

@Override
protected void onPostExecute(Void result) {
    super.onPostExecute(result);
    getCbtId(sqCbtId);
    Log.d(TAG, "onPostExecuteAsync: " + sqCbtId);
}

public void getCbtId(long cbtId) {
    sqCbtId = cbtId;
}

CbtId is being capture correctly in the log.d but its not sending back to the activity. 在log.d中正确捕获了CbtId,但未将其发送回活动。 I think it may be something to do with the constructor in the Async task. 我认为这可能与异步任务中的构造函数有关。

Modify your insert method 修改您的插入方法

public MutableLiveData<Long> insert(Note note) {
 final MutableLiveData<Long> id = new MutableLiveData<>();
 new InsertNoteAsyncTask(noteDao, id).execute(note); 
 return id;
}

Modify InsertNoteAsyncTask construct and receive the id . 修改InsertNoteAsyncTask构造并接收id Now modify the onPostExecute method and set the id value 现在修改onPostExecute方法并设置id

   class InsertNoteAsyncTask extends AsyncTask<Note, Void, Long> {
    private NoteDao noteDao;
    private MutableLiveData<Long> id;
     public InsertNoteAsyncTask(NoteDao noteDao, MutableLiveData<Long> id) {
        this.noteDao = noteDao;
        this.id = id;
     }

     @Override
     protected Long doInBackground(Note... notes) {
        long sqCbtId = noteDao.insert(notes[0]);
        return sqCbId;
     }

     @Override
     protected void onPostExecute(Long sqCbtId) {
        super.onPostExecute(result);
        id.setValue(sqCbtId); 
        Log.d(TAG, "onPostExecuteAsync: " + sqCbtId);
     }
    }

Now in ViewModel return the MutableLiveData and observe it in Activity. 现在在ViewModel中返回MutableLiveData并在Activity中进行观察。 for eg: 例如:

public LiveData<Long> insertNote(Note note) {
  return noteRepository.insert(note);
}

Now in Activity , you can observe the change in the LiveData : 现在在Activity ,您可以观察LiveData中的LiveData

viewModel.insertNote(Note).observe(this,
        new Observer<Long>() {
          @Override
          public void onChanged(Long id) {
            // do whatever you want with the id
          }
        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM