我正在使用MVP学习Dagger2,在阅读并尝试了许多教程之后,我使用默认构造函数引用了我的一个实例,并且一切正常。

但是,现在我正在从API调用中检索列表对象,并希望使用Dagger2将其传递给我的ArrayAdapter

在我的ListViewPresenter类中,在检索到值之后,我将其传递回ListView_View类,如下所示:view.populateListView(mealResponse);

然后在ListView_View类中,我通常会执行以下操作:

@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);
}


我对于如何使用Dagger2获取对List<Meal> meal对象的引用感到困惑,以便可以将其传递给构造函数。

这就是我设置Dagger2的方式。

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

public interface AppComponent{

    void inject(DaggerApplication application);

    void inject(BaseActivity baseActivity);
}


以下是依赖项的集合

@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);
  }
}


这是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;
    }
}


最后,我有BaseActivity,所有活动都从此开始,需要使用匕首:

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);
    }
}


编辑**我已经添加了我的适配器类

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;

    }
}

最佳答案

只需提供一个空列表:

@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);
    }
}


然后,在您的MealListAdapter类中,实现一个updateList方法以刷新Adapter内容:

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

07-28 04:50