我有这样的改造服务
public interface BrandsService { @GET("ListBrand") Call<List<Brand>> getBrands();}
然后我有一个Repository来从这样的API获取数据
public class BrandsRepository { public static final String TAG = "BrandsRepository"; mutablelivedata<List<Brand>> mutablelivedata; Retrofit retrofit; @Inject public BrandsRepository(Retrofit retrofit) { this.retrofit = retrofit; } public liveData<List<Brand>> getlistofBrands() {// Retrofit retrofit = apimanager.getAdapter(); final BrandsService brandsService = retrofit.create(BrandsService.class); Log.d(TAG, "getlistofBrands: 00000000000 "+retrofit); mutablelivedata = new mutablelivedata<>(); Call<List<Brand>> retrofitCall = brandsService.getBrands(); retrofitCall.enqueue(new Callback<List<Brand>>() { @OverrIDe public voID onResponse(Call<List<Brand>> call, Response<List<Brand>> response) { mutablelivedata.setValue(response.body()); } @OverrIDe public voID onFailure(Call<List<Brand>> call, Throwable t) { t.printstacktrace(); } }); return mutablelivedata; }}
我通过像这样注入Retrofit来使用Dagger2的构造函数注入.
然后我有一个像这样的viewmodel
public class Brandsviewmodel extends viewmodel{ BrandsRepository brandsRepository; liveData<List<Brand>> brandsliveData; @Inject public Brandsviewmodel(BrandsRepository brandsRepository) { this.brandsRepository = brandsRepository; } public voID callService(){ brandsliveData = brandsRepository.getlistofBrands(); } public liveData<List<Brand>> getBrandsliveData() { return brandsliveData; }}
要在BrandsRepository中注入Retrofit,我必须像这样注入BrandsRepository.
然后我有这样的MainActivity
public class MainActivity extends AppCompatActivity { private static final String TAG = "MainActivity"; @Inject Brandsviewmodel brandsviewmodel; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); ((MainApplication)getApplication()).getNetComponent().inject(this);// Brandsviewmodel brandsviewmodel = viewmodelProvIDers.of(this).get(Brandsviewmodel.class); brandsviewmodel.callService(); liveData<List<Brand>> brandsliveData = brandsviewmodel.getBrandsliveData(); brandsliveData.observe(this, new Observer<List<Brand>>() { @OverrIDe public voID onChanged(@Nullable List<Brand> brands) { Log.d(TAG, "onCreate: "+brands.get(0).getname()); } }); }}
使用Dagger2而不是viewmodelProvIDers注入Brandsviewmodel.这工作正常,但当我尝试通过取消注释使用viewmodelProvIDers时,dagger给了我很明显的错误.获取viewmodel的正确方法是使用viewmodelProvIDers,但是如何在注入这样的改进时完成此 *** 作.
解决方法:
答案可能比Mumi的方法更简单,即您在组件上公开viewmodel:
@Singleton@Component(modules={...})public interface SingletonComponent { Brandsviewmodel brandsviewmodel();}
现在,您可以在viewmodelFactory中的组件上访问此方法:
// @InjectBrandsviewmodel brandsviewmodel;...brandsviewmodel = viewmodelProvIDers.of(this, new viewmodelProvIDer.Factory() { @OverrIDe public <T extends viewmodel> create(Class<T> modelClazz) { if(modelClazz == Brandsviewmodel.class) { return singletonComponent.brandsviewmodel(); } throw new IllegalArgumentException("Unexpected class: [" + modelClazz + "]"); }).get(Brandsviewmodel.class);
所有这些都可以通过Kotlin简化和隐藏:
inline fun <reifIEd T: viewmodel> AppCompatActivity.createviewmodel(crossinline factory: () -> T): T = T::class.java.let { clazz -> viewmodelProvIDers.of(this, object: viewmodelProvIDer.Factory { overrIDe fun <T : viewmodel?> create(modelClass: Class<T>): T { if(modelClass == clazz) { @Suppress("UNCHECKED_CAST") return factory() as T } throw IllegalArgumentException("Unexpected argument: $modelClass") } }).get(clazz)}
现在可以让你做到
brandsviewmodel = createviewmodel { singletonComponent.brandsviewmodel() }
现在Brandsviewmodel可以从Dagger接收其参数:
class Brandsviewmodel @Inject constructor( private val appContext: Context, /* other deps */): viewmodel() { ...}
总结 以上是内存溢出为你收集整理的android – ViewModelProviders with Dagger 2,无法掌握这个概念全部内容,希望文章能够帮你解决android – ViewModelProviders with Dagger 2,无法掌握这个概念所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)