将数据从ViewModel的AsyncTask传递到调用活动

时间:2018-01-12 04:51:36

标签: java android android-viewmodel

我正在使用Room向我的数据库添加一个项目。

我在AddFriendActivity中获取用户的详细信息,该文件调用 AddFriendViewModel addItem(),后者又调用子类AddAsyncTask

项目已插入,我在AsyncTask的 doInBackground() OnPostExecute()中获取了id。现在我需要在我的AddFriendActivity中传递或获取此id。

我的活动:

public class AddFriendActivity extends AppCompatActivity {

    private AddFriendViewModel addFriendViewModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        .
        .
        .
        addFriendViewModel = ViewModelProviders.of(this).get(AddFriendViewModel.class);

        FloatingActionButton fab = findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                addFriend();
            }
        });

    }

    private void addFriend() {
        .
        .
        .
        addFriendViewModel.addFriend(new FriendEntity(...));
        finish();
    }
}

我的带有子类AddAsyncTask的ViewModel类:

public class AddFriendViewModel extends AndroidViewModel {

    private AppDatabase appDatabase;

    public AddFriendViewModel(Application application) {
        super(application);
        appDatabase = AppDatabase.getDatabase(this.getApplication());
    }

    void addFriend(final FriendEntity friendEntity) {
        AddAsyncTask myTask = new AddAsyncTask(appDatabase);
        myTask.execute(friendEntity);
    }

    private static class AddAsyncTask extends AsyncTask<FriendEntity, Void, Long> {

        private AppDatabase db;

        AddAsyncTask(AppDatabase appDatabase) {
            db = appDatabase;
        }

        @Override
        protected Long doInBackground(final FriendEntity... params) {
            return db.friendDao().insert(params[0]);
        }

        @Override
        protected void onPostExecute(Long insertedId) {

        }
    }
}

1 个答案:

答案 0 :(得分:1)

添加界面

  public interface IdCallback{
     void onFriendInserted(Long id);
    }

在AddFriendActivity中实现它

class AddFriendActivity extends AppCompatActivity implements IdCallback{
private AddFriendViewModel addFriendViewModel;

@Override
protected void onCreate(Bundle savedInstanceState) {
    .
    .
    .
    addFriendViewModel = ViewModelProviders.of(this).get(AddFriendViewModel.class);

    FloatingActionButton fab = findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            addFriend();
        }
    });

}

private void addFriend() {
    .
    .
    .
    addFriendViewModel.addFriend(new FriendEntity(...),this);//<-- pass this
    finish();
}

 public void onFriendInserted(Long id){
  // do something with id
 } 
...

}

interface传递给我们的ViewModel via构造函数,并在返回时调用此ID

 public class AddFriendViewModel extends AndroidViewModel {

    private AppDatabase appDatabase;
    private IdCallback callback;

    public AddFriendViewModel(Application application) {
        super(application);
        appDatabase = AppDatabase.getDatabase(this.getApplication());
    }

    void addFriend(final FriendEntity friendEntity,IdCallback callback) {
        AddAsyncTask myTask = new AddAsyncTask(appDatabase);
        this.callback = callback; //< assign
        myTask.execute(friendEntity);
    }

    private static class AddAsyncTask extends AsyncTask<FriendEntity, Void, Long> {

        private AppDatabase db;

        AddAsyncTask(AppDatabase appDatabase) {
            db = appDatabase;
        }

        @Override
        protected Long doInBackground(final FriendEntity... params) {
            return db.friendDao().insert(params[0]);
        }

        @Override
        protected void onPostExecute(Long insertedId) {
            callback.onFriendInserted(insertedId);
        }
    }
}