https://kotlinlang.org logo
Title
s

sidharthanil

06/28/2017, 1:42 PM
Hi, I have a doubt, How do we call an interface method in fragment from parent activity in kotlin. When I try to do that it says the interface object must be initialised
g

gildor

06/28/2017, 1:48 PM
Please share code example
s

sidharthanil

06/28/2017, 2:06 PM
I created an interface in mainactivity
interface onProductsChangedListner{
        fun updateProductList(products:List<Product>)
}
and implemented the same in a fragment
class GalleryFragment: Fragment(),MainActivity.onProductsChangedListner {
    override fun updateProductList(products: List<Product>) {
        this.products = products
        galleryAdapter.changeDataset(products)
    }
How do i call the interface methods from main activity?
When i try to create an object of interface class, kotlin says it must be initialised. It is not required in java right?. So what should i do?
g

gildor

06/28/2017, 2:22 PM
How do you call method from activity?
s

sidharthanil

06/28/2017, 2:34 PM
I tried doing this
var productChangedListner: onProductsChangedListner
productChangedListner.updateProductList(products)
but it says productChangedListner must be initialised
g

gildor

06/28/2017, 2:38 PM
But looks like you try to access the same property that initializing (also, where is equals between type and assignment)
Where is fragment in this assignment?
s

sidharthanil

06/28/2017, 2:41 PM
Im implementing the interface in fragment
g

gildor

06/28/2017, 4:26 PM
And? Maybe could you just post screenshot from your IDE with error message and your code. Because your code snippet above is just syntactically wrong
s

sidharthanil

06/29/2017, 5:26 AM
I figured it out. I guess it was syntactically wrong. Thank you for pointingthat out