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