gpt4 book ai didi

android - 使用 Dagger2 将检索到的对象传递给构造函数

转载 作者:行者123 更新时间:2023-11-29 23:31:17 25 4
gpt4 key购买 nike

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

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

在我的 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);
}

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

这就是我设置 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,其中所有 Activity 都需要 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);
}
}

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

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

关于android - 使用 Dagger2 将检索到的对象传递给构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52630817/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com