abc
abc

Reputation: 93

Passing a retrieved object to constructor using Dagger2

I am learning Dagger2 with MVP and after reading and attempting lots of tutorials, I have made reference to one of my instances using the default constructor and everything works great.

However, now I am retrieving a list object from an API call and wish to pass it to my ArrayAdapter using Dagger2.

In my ListViewPresenter class, after retrieving the value, I pass it back to the ListView_View class as so: view.populateListView(mealResponse);

Then in ListView_View class, I would normally do the following:

@Override
public void populateListView(final List<Meal> meal) {

    MealListAdapter cosmeticAdapter = new MealListAdapter(this, meal);
    final ListView listView = (ListView) findViewById(R.id.list_view);
    listView.setAdapter(cosmeticAdapter);
}

I am confused regarding how to get the reference to my List<Meal> meal object with Dagger2, so that it can be passed to the constructor.

This is how I am setting up Dagger2.

//Component binds our dependencies
@Singleton
@Component(modules = {AppModule.class})

public interface AppComponent{

    void inject(DaggerApplication application);

    void inject(BaseActivity baseActivity);
}

Below is the collection of dependencies

@Module
public class AppModule {

private final DaggerApplication application;

//This is where context is being defined
public AppModule(DaggerApplication app){
    this.application = app;
  }

@Provides
@Singleton
Context providesApplicationContext(){

    return application;
  }

@Provides
public ListViewPresenter providesListviewPresenter() {
    return new ListViewPresenter();
  }


@Provides
public MealListAdapter providesMealListAdapter(List<Meal> meal) {
    return new MealListAdapter(application, meal);
  }
}

Here is the entry point for Dagger2

//An application class is the entry point for the app (from a cold start), this is referenced in the Android Manifest
public class DaggerApplication extends Application {

    //Here is where the AppComponent is handled from the AppComponent interface class
    AppComponent appComponent;

    @Override
    public void onCreate(){

        super.onCreate();
    /*
    Here we feed the dagger builder the reference point to what it should be instantiating or provided,
    which is found in the AppModule Class.

    'this' is being passed to the constructor found in AppModule Class, so reference can be passed
     */
        appComponent = DaggerAppComponent.builder().appModule(new AppModule(this)).build();

        //This is passed as we are injecting into 'this' activity
        appComponent.inject(this);
    }

    /*
Below is a helper method;
We are returning the app component here so that the base activity can get references to the AppComponent
this will allow us to inject anywhere
 */
    public AppComponent getAppComponent(){
        return  appComponent;
    }
}

Finally, I have my BaseActivity where all activities extend from that require dagger:

public class BaseActivity extends AppCompatActivity {

    @Inject public
    ListViewPresenter presenter;
    @Inject public
    MealListAdapter cosmeticAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        /*
        Here we are saying cast the getApplication() from the DaggerApplication, get the app component from it,
        because in this case we already have the appComponent defined in the DaggerApplication class as
        we are first injecting from the DaggerApplication class
         */

        ((DaggerApplication) getApplication()).getAppComponent().inject(this);
    }
}

Edit** I have added my adapter class

public class MealListAdapter extends ArrayAdapter<Meal> {


    public MealListAdapter(@NonNull Context context, List<Meal> mealArrayList) {
        super(context,0,mealArrayList);
    }


    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

        // Get the data item for this position
        Meal meal = getItem(position);
        // Check if an existing view is being reused, otherwise inflate the view
        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.meal_list_layout, parent, false);
        }
        // Lookup view for data population
        ImageView iv_cosmetic = (ImageView) convertView.findViewById(R.id.iv_meal);
        TextView tv_cosmetic = (TextView) convertView.findViewById(R.id.tv_meal);

        // Populate the data into the template view using the data object
        Picasso.get().load(meal.getStrMealThumb()).into(iv_cosmetic);
        tv_cosmetic.setText(meal.getStrMeal());

        // Return the completed view to render on screen
        return convertView;

    }
}

Upvotes: 1

Views: 288

Answers (1)

AlexTa
AlexTa

Reputation: 5251

Simply provide an empty list:

@Module
public class AppModule {

    private final DaggerApplication application;

    //This is where context is being defined
    public AppModule(DaggerApplication app){
        this.application = app;
    }

    @Provides
    @Singleton
    Context providesApplicationContext(){
        return application;
    }

    @Provides
    public ListViewPresenter providesListviewPresenter() {
        return new ListViewPresenter();
    }

    // Provide an empty meal list
    @Provides
    public List<Meal> providesMealList() {
        return new ArrayList<Meal>();
    }

    @Provides
    public MealListAdapter providesMealListAdapter(List<Meal> meal) {
        return new MealListAdapter(application, meal);
    }
}

Then, in your MealListAdapter class, implement an updateList method to refresh Adapter content:

public void updateList(List<Meal> meal) {
    this.meal = meal;
    notifyDataSetChanged();
}

Upvotes: 1

Related Questions