Java – dagger 2: error getting multiple instances of the same object using @ named
How can I get multiple instances of the same return type like a cursor
For example:-
Module @CursorScope public class CursorModule { @Provides Cursor provideSongCursor( @Named("Song") Musician musician) { return musician.getApplicationContext().getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,new String[]{ BaseColumns._ID,MediaStore.Audio.AudioColumns.TITLE,MediaStore.Audio.AudioColumns.ARTIST,MediaStore.Audio.AudioColumns.ALBUM,MediaStore.Audio.AudioColumns.DURATION },MediaStore.Audio.AudioColumns.IS_MUSIC + "=1",null,null); } @Provides Cursor provideAlbumCursor( @Named("Album") Musician musician) { return musician.getApplicationContext().getContentResolver().query(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI,MediaStore.Audio.AlbumColumns.ALBUM,MediaStore.Audio.AlbumColumns.ARTIST,MediaStore.Audio.AlbumColumns.NUMBER_OF_SONGS,MediaStore.Audio.AlbumColumns.FIRST_YEAR },null); } @Provides Cursor provideArtistCursor(@Named("Artist") Musician musician) { return musician.getApplicationContext().getContentResolver().query(MediaStore.Audio.Artists.EXTERNAL_CONTENT_URI,new String[] { BaseColumns._ID,MediaStore.Audio.ArtistColumns.ARTIST,MediaStore.Audio.ArtistColumns.NUMBER_OF_ALBUMS,MediaStore.Audio.ArtistColumns.NUMBER_OF_TRACKS },null); } @Provides Cursor provideGenreCursor( @Named("Genres") Musician musician) { return musician.getApplicationContext().getContentResolver().query(MediaStore.Audio.Genres.EXTERNAL_CONTENT_URI,MediaStore.Audio.GenresColumns.NAME },null); } @Provides Cursor providePlaylistCursor(@Named("Playlist") Musician musician) { return musician.getApplicationContext().getContentResolver().query(MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI,MediaStore.Audio.PlaylistsColumns.NAME },null); } }
Provided
@CursorScope @Subcomponent(modules = CursorModule.class) public interface CursorComponent { Cursor cursor(); }
I received this error
Error:(17,11) Gradle: error: android.database.Cursor is bound multiple times: @Provides android.database.Cursor com.merkmod.musician.dependency.CursorModule.provideSongCursor(@Named("Song") com.merkmod.musician.application.Musician) @Provides android.database.Cursor com.merkmod.musician.dependency.CursorModule.provideAlbumCursor(@Named("Album") com.merkmod.musician.application.Musician) @Provides android.database.Cursor com.merkmod.musician.dependency.CursorModule.provideArtistCursor(@Named("Artist") com.merkmod.musician.application.Musician) @Provides android.database.Cursor com.merkmod.musician.dependency.CursorModule.provideGenreCursor(@Named("Genres") com.merkmod.musician.application.Musician) @Provides android.database.Cursor com.merkmod.musician.dependency.CursorModule.providePlaylistCursor(@Named("Playlist") com.merkmod.musician.application.Musician)
I created multiple cursor instances and annotated them with @ named at the provider level. Then it started giving me errors and couldn't provide @ provides annotation, so I used it in the constructor instead
Just like the code above The problem is to run a loop again and again. I'm like falling into the task of completing the cursor. Any help will be applied
resolvent
Solution
When you want to provide multiple variables of one type, you must use the @ named annotation as follows:
Module @CursorScope public class CursorModule { @Provides @Named("songCursor") Cursor provideSongCursor( @Named("Song") Musician musician) { return musician.getApplicationContext().getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,new String[]{ BaseColumns._ID,MediaStore.Audio.AudioColumns.DURATION },null); } @Provides @Named("albumCursor") Cursor provideAlbumCursor( @Named("Album") Musician musician) { return musician.getApplicationContext().getContentResolver().query(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI,MediaStore.Audio.AlbumColumns.FIRST_YEAR },null); } @Provides @Named("artistCursor") Cursor provideArtistCursor(@Named("Artist") Musician musician) { return musician.getApplicationContext().getContentResolver().query(MediaStore.Audio.Artists.EXTERNAL_CONTENT_URI,new String[] { BaseColumns._ID,MediaStore.Audio.ArtistColumns.NUMBER_OF_TRACKS },null); } @Provides @Named("genreCursor") Cursor provideGenreCursor( @Named("Genres") Musician musician) { return musician.getApplicationContext().getContentResolver().query(MediaStore.Audio.Genres.EXTERNAL_CONTENT_URI,MediaStore.Audio.GenresColumns.NAME },null); } @Provides @Named("playListCursor") Cursor providePlaylistCursor(@Named("Playlist") Musician musician) { return musician.getApplicationContext().getContentResolver().query(MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI,MediaStore.Audio.PlaylistsColumns.NAME },null); } }
So when you want to inject like this:
@Inject @Named("soundCursor") Cursor soundCursor; @Inject @Named("albumCursor") Cursor albumCursor; @Inject @Named("artistCursor") Cursor artistCursor; @Inject @Named("genreCursor") Cursor genreCursor; @Inject @Named("playListCursor") Cursor playListCursor;
If you want to inject them in the constructor injection, do the following:
@Inject public SomeClassConstructor(@Named("album") Cursor cursur)
And what you write in your sub component interface. I can't get what it is. It must be like:
@CursorScope @Subcomponent(modules = CursorModule.class) public interface CursorComponent { void inject(TheClassThatWantsToUseInject1 obj); void inject(TheClassThatWantsToUseInject2 obj); }
In your application components:
YourSubComponentInterface plus(CursorModule module);