1

I am creating Adapter (for Recyclerview) using MVVM structure in Android, where I have created ViewModel class for each item of recyclerview. now I want to call an API on Onclick() method of recyclerview in android.

I cannot call API in recyclerview's itemViewModel so i have to call the api using Activity view Model.

what is the best way to do this?

i am following this example on git: https://github.com/MindorksOpenSource/android-mvvm-architecture/tree/master/app/src/main/java/com/mindorks/framework/mvvm

Akshay
  • 111
  • 1
  • 12
  • which API please explain more... please write more details about the problem. – Itzik Samara Feb 08 '19 at 15:05
  • @ItzikSamara i want to call my web service on item click. but the problem is that the code to call web service is inside ActivityViewModel. example:https://github.com/MindorksOpenSource/android-mvvm-architecture/blob/master/app/src/main/java/com/mindorks/framework/mvvm/ui/feed/blogs/BlogAdapter.java the way it has open the browser on onclick. – Akshay Feb 08 '19 at 15:10
  • 2
    Pass the view model object from your activity to your adapter and then call whatever api you want, alternatively you can use something like event bus and post events to receive in view model. – Roshaan Farrukh Feb 08 '19 at 15:38
  • @RoshaanFarrukhI have injected the adapter in activity. but cannot pass anything as a constructor. you can refer the structure from the above GitHub link – Akshay Feb 08 '19 at 15:49
  • @RoshaanFarrukh thanks for suggestion I passed activity reference in the adapter and it worked – Akshay Feb 09 '19 at 09:37

0 Answers0