Передача извлеченного объекта в конструктор с использованием Dagger2 - PullRequest
0 голосов
/ 03 октября 2018

Я изучаю Dagger2 с помощью MVP, и после прочтения и пробования множества учебных пособий, я сделал ссылку на один из моих примеров, используя конструктор по умолчанию, и все отлично работает.

Однако теперь я извлекаю объект списка из вызова 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, где все действия расширяются от того, что требует кинжал:

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;

    }
}

1 Ответ

0 голосов
/ 03 октября 2018

Просто предоставьте пустой список:

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