我做了一个学习匕首的简单项目。该应用程序正在从互联网上获取房产(度假租赁)列表,并将其显示在
RecyclerView
列表我用Dagger 2注入了所有依赖项,但列表的适配器除外。适配器非常标准,它接受属性列表并填充视图:
public class PropertyListAdapter extends RecyclerView.Adapter<PropertyListAdapter.ViewHolder> {
private List<Property> mPropertyList;
@Inject
public PropertyListAdapter(List<Property> propertyList) {
mPropertyList = propertyList;
}
// onCreateViewHolder()
// onBindViewHolder()
// getItemCount()
// class ViewHolder{}
}
该应用程序由一个活动组成,其中包含一个包含
recyclerview
列表在片段的
onActivityCreated()
我有:
mPropertyViewModel.getPropertyList().observe(this, properties -> setPropertyAdapter(properties));
以及
setPropertyAdapter()
是:
private void setPropertyAdapter(List<Property> properties) {
rvPropertyList.setAdapter(new PropertyListAdapter(properties));
}
所以,注入
PropertyListAdapter
我创建了一个模块:
@Module
public class AdapterModule {
@Provides
@Singleton
PropertyListAdapter providePropertyListAdapter(List<Property> propertyList) {
return new PropertyListAdapter(propertyList);
}
}
但后来我意识到我必须注射
List<Property>
我不知道如何做到这一点,我陷入了困境。在本例中,如何注入适配器?
P、 我将MVVM与架构组件一起使用。
日志类别:
error: [dagger.android.AndroidInjector.inject(T)] java.util.List<com.aandritchi.android.propertyrentals.data.domain.Property> cannot be provided without an @Provides-annotated method.
java.util.List<com.aandritchi.android.propertyrentals.data.domain.Property> is injected at
com.aandritchi.android.propertyrentals.di.module.data.AdapterModule.providePropertyListAdapter(propertyList)
com.aandritchi.android.propertyrentals.ui.search_result.PropertyListAdapter is injected at
com.aandritchi.android.propertyrentals.ui.search_result.PropertySearchResultFragment.mPropertyListAdapter
com.aandritchi.android.propertyrentals.ui.search_result.PropertySearchResultFragment is injected at
com.aandritchi.android.propertyrentals.ui.home.HomeActivity.mPropertySearchResultFragment
com.aandritchi.android.propertyrentals.ui.home.HomeActivity is injected at
dagger.android.AndroidInjector.inject(arg0)