如何使用Kotlin调用返回模块类型的方法?

时间:2018-09-27 04:27:47

标签: android kotlin kotlin-android-extensions kotlin-extension kodein

我有两个文件one.kttwo.kt,我在one.kt文件中有内部类。我想在Two.kt文件中执行某些操作(如单击按钮)时调用该内部类。我尝试了一些调用内部类但不调用方法的方法。请指导我有什么错误。 One.kt文件中的manifest文件调用。像这样

<application
        android:name="com.abc.cde.One"

One.kt:

open class One : MultiDexApplication(), KodeinAware {
    ---------------
    --------------
    OnCreate()
    ---------------
    ---------------

inner class CallmyClass() {

        fun CallMyMethod(i: Int) :Module {

            return Module {
                bind<ExchangeRateProvider>() with singleton { CryptoCompareExchangeProvider(this@One, instance()) }
                bind<SyncProgressProvider>() with singleton { SyncProgressProvider() }
                bind<WallethKeyStore>() with singleton { keyStore }
                bind<Settings>() with singleton { KotprefSettings }

                bind<CurrentTokenProvider>() with singleton { CurrentTokenProvider(instance()) }

                bind<AppDatabase>() with singleton { Room.databaseBuilder(applicationContext, AppDatabase::class.java, "maindb").build() }
                bind<NetworkDefinitionProvider>() with singleton { NetworkDefinitionProvider(instance()) }
                //bind<CurrentAddressProvider>() with singleton { InitializingCurrentAddressProvider(keyStore, instance(), instance(), applicationContext) }
                bind<CurrentAddressProvider>() with singleton { InitializingCurrentAddressProvider(keyStore, instance(), instance(), applicationContext,i) }

                bind<FourByteDirectory>() with singleton { FourByteDirectoryImpl(instance(), applicationContext) }
            }
        }
    }
}

Two.kt:

class Two : AppCompatActivity(), KodeinAware {

    -----------
    -----------
    -----------

    oncreate()

    -------------
    --------------

     val dd = One().CallmyClass().CallMyMethod(1)
}

一些博客说您可以使用此方法来调用这种类型的方法,但是我不知道如何在Activity类中使用它,因为它不会扩展Application类。

    override val kodein = Kodein.lazy {
           import(CallMyMethod(1))
    }

我将此代码包含在我的One.kt文件中,我想从Two.kt文件中调用吗?请问我

  

是吗?像这样调用val dd = One()。CallmyClass()。CallMyMethod(1)。但是由于上面的方法是使用KodeinAware返回模块类型,所以它没有调用我的方法。

参考:https://proandroiddev.com/dependency-injection-with-kotlin-kodein-koin-3d783745e48d

更新:

private fun CallMyMethod(i: Int) :Module {
                return Module {

                    //bind<ExchangeRateProvider>() with singleton { CryptoCompareExchangeProvider(this@App, instance()) }
                    bind<ExchangeRateProvider>(overrides = true) with singleton { CryptoCompareExchangeProvider(this@App, instance()) }

                    //bind<SyncProgressProvider>() with singleton { SyncProgressProvider() }
                    bind<SyncProgressProvider>(overrides = true) with singleton { SyncProgressProvider() }

                    //bind<WallethKeyStore>() with singleton { keyStore }
                    bind<WallethKeyStore>(overrides = true) with singleton { keyStore }

                    //bind<Settings>() with singleton { KotprefSettings }
                    bind<Settings>(overrides = true) with singleton { KotprefSettings }

                    //bind<CurrentTokenProvider>() with singleton { CurrentTokenProvider(instance()) }
                    bind<CurrentTokenProvider>(overrides = true) with singleton { CurrentTokenProvider(instance()) }

                    //bind<AppDatabase>() with singleton { Room.databaseBuilder(applicationContext, AppDatabase::class.java, "maindb").build() }
                    bind<AppDatabase>(overrides = true) with singleton { Room.databaseBuilder(applicationContext, AppDatabase::class.java, "maindb").build() }

                    //bind<NetworkDefinitionProvider>() with singleton { NetworkDefinitionProvider(instance()) }
                    bind<NetworkDefinitionProvider>(overrides = true) with singleton { NetworkDefinitionProvider(instance()) }

                    //bind<CurrentAddressProvider>() with singleton { DuplicateInitializingCurrentAddressProvider(keyStore, instance(), instance(), applicationContext,1) }
                    bind<CurrentAddressProvider>(overrides = true) with singleton { DuplicateInitializingCurrentAddressProvider(keyStore, instance(), instance(), applicationContext,1) }

                    //bind<FourByteDirectory>() with singleton { FourByteDirectoryImpl(instance(), applicationContext) }
                    bind<FourByteDirectory>(overrides = true) with singleton { FourByteDirectoryImpl(instance(), applicationContext) }
                }
            }

            override val kodein = Kodein {
                //import(CallMyMethod(1), allowOverride = true)
                import(CallMyMethod(1))
            }

0 个答案:

没有答案