2017-12-27 27 views
0

は私がモデルmouduleを作成MVP + dagger2モデルをPresenterに挿入するにはどうすればいいですか?

を作成してみてください:

@Module 
class ModelsModule { 

    @Provides 
    BasketModel provideBasketModel() { 
     return new BasketModel(); 
    } 

    @Provides 
    ProductModel provideProductModel() { 
     return new ProductModel(); 
    } 
} 

と私はプレゼンターを作成する必要があります。私のプレゼンターは、モデルに

プレゼンターを使用する必要があります。

public class ProductPresenter { 

    private ProductModel; 

    public ProductPresenter(ProductModel productModel) { 
     this.productModel = productModel; 
    } 

    publict void test(){ 
     productModel.someMethod(); 
    } 

とプレゼンターを作成するとき、私はProductModelを設定することはできません。私はこのように作成プレゼンター:

@Module 
public class PresentersModule { 

    @Provides 
    ProductPresenter provideProductPresenter() { 
     return new ProductPresenter();//What I need set to constructor? new ProductModel()? 
    } 
+0

コンポーネントはどこにありますか?どのようにコンポーネントを構築していますか? – azizbekian

答えて

1

あなたがプレゼンタークラスでProductModelを渡しているので、あなたもあなたのプレゼンターを構築する方法をごPresenterModuleを伝える必要があります:

@Module 
public class PresentersModule { 

@Provides 
ProductPresenter provideProductPresenter(ProductModel model) { 
    return new ProductPresenter(model); 
    } 
} 

ダガーがいることを知るには十分賢いですモデルインスタンスを別の@Moduleクラスに既にビルドしています。

@Inject 
public ProductPresenter(ProductModel productModel) { 
    this.productModel = productModel; 
} 

EDIT:

私はあなたにも同じよう@Injectであなたのプレゼンターのコンストラクタに注釈を付ける必要があると思うと明らかにあなたは@Componentインタフェースを必要としています。あなたは関連するコードを投稿していませんが、あなたはそれを持っていると思います。

関連する問題