I made a simple project for learning Dagger. The app is fetching a list of properties (vacation rentals) from Internet and displays them on a RecyclerView
list. I injected all dependencies with Dagger 2 except the adapter for the list. The adapter is pretty standard, it takes a list of properties and populates the views:
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{}
}
The app consists of one activity that holds a fragment which contains the recyclerview
list. In the fragment's onActivityCreated()
I have:
mPropertyViewModel.getPropertyList().observe(this, properties -> setPropertyAdapter(properties));
and the setPropertyAdapter()
is:
private void setPropertyAdapter(List<Property> properties) {
rvPropertyList.setAdapter(new PropertyListAdapter(properties));
}
So, to inject PropertyListAdapter
I have created a module:
@Module
public class AdapterModule {
@Provides
@Singleton
PropertyListAdapter providePropertyListAdapter(List<Property> propertyList) {
return new PropertyListAdapter(propertyList);
}
}
but then I realized that I have to inject the List<Property>
and I don't know how to achieve this and I'm stuck. How to inject the adapter in this example?
P.S. I'm using MVVM with architecture components.
Logcat: 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)