1

我正在使用 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();
    }
}

我的 ViewModel 类与子类 AddAsyncTask:

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) {

        }
    }
}
4

1 回答 1

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给我们的ViewModelvia 构造函数并在取回 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);
        }
    }
}
于 2018-01-12T05:01:53.120 回答