本文介绍了如何在Android中使用Dagger2将字符串传递给ViewModel / Repository类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的片段类:
public class MyFragment extends DaggerFragment {
@Inject MyViewModelFactory factory;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle) {
String uid = "123"; //How to pass this String??
MyViewModel viewModel = ViewModelProviders.of(this, factory).get(MyViewModel.class);
LiveData<User> liveData = viewModel.getUserLiveData();
}
}
现在这是 MyViewModel
类:
public class MyViewModel extends ViewModel {
private MutableLiveData<User> liveData;
@Inject
MyViewModel(MyRepository repository) {
userLiveData = repository.addUserToLiveData();
//Here I need the value of that String "123"
}
LiveData<User> getUserLiveData() {
return liveData;
}
}
还有这个 MyRepository
类:
@Singleton
class MyRepository {
private Retrofit retrofit;
@Inject
MyRepository(Retrofit retrofit) {
//Or here I need the value of that String "123"
}
MutableLiveData<User> addUserToLiveData() {
//Make api call using retrofit
}
}
也请看看我的 AppModule
类:
@Module
public class AppModule {
@Singleton
@Provides
static Retrofit provideRetrofit(){
return new Retrofit.Builder()... //Initialize retrofit
}
}
在 MyViewModelModule
类:
@Module
abstract class MyViewModelModule {
@Binds
abstract ViewModelProvider.Factory bindMyViewModelFactory(MyViewModelFactory factory);
@Binds
@IntoMap
@ViewModelKey(MyViewModel.class)
abstract ViewModel provideMyViewModel(MyViewModel viewModel);
}
我试图在ViewModel / repository构造函数中添加字符串,但是没有任何运气。请帮助我将 uid
传递给ViewModel或存储库类。谢谢!
What I have tried is to add the string in the ViewModel/repository constructor but without any luck. Please help me pass that uid
to the ViewModel or repository class. Thanks!
推荐答案
一个可能的解决方案是每个视图模型有1个工厂,然后将UID设置为工厂,然后工厂通过
A possible solution is to have 1 factory per viewmodel, then you set the UID to the factory and the factory passes it to the viewmodel
class MyViewModelFactory @Inject constructor(
private val repository: Repository
): ViewModelProvider.Factory {
lateinit var uid: String
@Suppress("UNCHECKED_CAST")
override fun <T : ViewModel> create(modelClass: Class<T>): T =
if (modelClass.isAssignableFrom(MyViewModel::class.java)) {
MyViewModel(
repository,
uid
) as T
} else {
throw IllegalArgumentException("Unknown ViewModel class [$modelClass]")
}
}
然后
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle) {
String uid = "123"; //How to pass this String??
factory.setUid(uid);
MyViewModel viewModel = ViewModelProviders.of(this, factory).get(MyViewModel.class);
LiveData<User> liveData = viewModel.getUserLiveData();
}
编辑:在Java中
public final class MyViewModelFactory implements Factory {
private final Repository repository;
private String uid;
@Inject
public MyViewModelFactory(Repository repository) {
this.repository = repository;
}
@NotNull
public ViewModel create(@NotNull Class modelClass) {
if (modelClass.isAssignableFrom(MyViewModel.class)) {
return new MyViewModel(repository, uid);
} else {
throw (Throwable)(new IllegalArgumentException("Unknown ViewModel class [" + modelClass + ']'));
}
}
public void setUid(@NonNull final String uid) {
this.uid = uid;
}
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle) {
String uid = "123"; //How to pass this String??
factory.setUid(uid);
MyViewModel viewModel = ViewModelProviders.of(this, factory).get(MyViewModel.class);
LiveData<User> liveData = viewModel.getUserLiveData();
}
这篇关于如何在Android中使用Dagger2将字符串传递给ViewModel / Repository类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!