Classifier does not have a companion object, and thus must be initialized here












1















Maybe this question is asked but not getting clue from the solutions which can be applied on my code.



The situation is:



I have an Activity where an interface is declared and I have a ViewModel class which has override interface and want to invoke the method of interface from Activity to make changes in ViewModel class but unable to call method in Activity saying ViewModel class does not have a companion object, and thus must be initialized here. How to resolve this?



var selection: setSelectionSubRow? = null
selection=RowSubTShirtViewModel
selection!!.setNameSelection(false)


above code is in Activity whose name is TShirtActivity.



below code is from RowViewModel class



class RowSubTShirtViewModel(private val subTShirtAdapter: SubTShirtAdapter, val context: TShirtActivity,
val tShirtBean: CommonItemBean, private val parentPosition: Int, private val position: Int) : BaseObservable() ,TShirtActivity.setSelectionSubRow{

fun getImageDrawable(): Drawable {
return if (tShirtBean.isSelected)
ContextCompat.getDrawable(context, R.drawable.green_border_circle)!!
else
ContextCompat.getDrawable(context, R.drawable.border_circle)!!
}

override fun setNameSelection(selection: Boolean) {
if (parentPosition == 6) {
if (position == 1) {
tShirtBean.isSelected = false
}
}
}









share|improve this question





























    1















    Maybe this question is asked but not getting clue from the solutions which can be applied on my code.



    The situation is:



    I have an Activity where an interface is declared and I have a ViewModel class which has override interface and want to invoke the method of interface from Activity to make changes in ViewModel class but unable to call method in Activity saying ViewModel class does not have a companion object, and thus must be initialized here. How to resolve this?



    var selection: setSelectionSubRow? = null
    selection=RowSubTShirtViewModel
    selection!!.setNameSelection(false)


    above code is in Activity whose name is TShirtActivity.



    below code is from RowViewModel class



    class RowSubTShirtViewModel(private val subTShirtAdapter: SubTShirtAdapter, val context: TShirtActivity,
    val tShirtBean: CommonItemBean, private val parentPosition: Int, private val position: Int) : BaseObservable() ,TShirtActivity.setSelectionSubRow{

    fun getImageDrawable(): Drawable {
    return if (tShirtBean.isSelected)
    ContextCompat.getDrawable(context, R.drawable.green_border_circle)!!
    else
    ContextCompat.getDrawable(context, R.drawable.border_circle)!!
    }

    override fun setNameSelection(selection: Boolean) {
    if (parentPosition == 6) {
    if (position == 1) {
    tShirtBean.isSelected = false
    }
    }
    }









    share|improve this question



























      1












      1








      1








      Maybe this question is asked but not getting clue from the solutions which can be applied on my code.



      The situation is:



      I have an Activity where an interface is declared and I have a ViewModel class which has override interface and want to invoke the method of interface from Activity to make changes in ViewModel class but unable to call method in Activity saying ViewModel class does not have a companion object, and thus must be initialized here. How to resolve this?



      var selection: setSelectionSubRow? = null
      selection=RowSubTShirtViewModel
      selection!!.setNameSelection(false)


      above code is in Activity whose name is TShirtActivity.



      below code is from RowViewModel class



      class RowSubTShirtViewModel(private val subTShirtAdapter: SubTShirtAdapter, val context: TShirtActivity,
      val tShirtBean: CommonItemBean, private val parentPosition: Int, private val position: Int) : BaseObservable() ,TShirtActivity.setSelectionSubRow{

      fun getImageDrawable(): Drawable {
      return if (tShirtBean.isSelected)
      ContextCompat.getDrawable(context, R.drawable.green_border_circle)!!
      else
      ContextCompat.getDrawable(context, R.drawable.border_circle)!!
      }

      override fun setNameSelection(selection: Boolean) {
      if (parentPosition == 6) {
      if (position == 1) {
      tShirtBean.isSelected = false
      }
      }
      }









      share|improve this question
















      Maybe this question is asked but not getting clue from the solutions which can be applied on my code.



      The situation is:



      I have an Activity where an interface is declared and I have a ViewModel class which has override interface and want to invoke the method of interface from Activity to make changes in ViewModel class but unable to call method in Activity saying ViewModel class does not have a companion object, and thus must be initialized here. How to resolve this?



      var selection: setSelectionSubRow? = null
      selection=RowSubTShirtViewModel
      selection!!.setNameSelection(false)


      above code is in Activity whose name is TShirtActivity.



      below code is from RowViewModel class



      class RowSubTShirtViewModel(private val subTShirtAdapter: SubTShirtAdapter, val context: TShirtActivity,
      val tShirtBean: CommonItemBean, private val parentPosition: Int, private val position: Int) : BaseObservable() ,TShirtActivity.setSelectionSubRow{

      fun getImageDrawable(): Drawable {
      return if (tShirtBean.isSelected)
      ContextCompat.getDrawable(context, R.drawable.green_border_circle)!!
      else
      ContextCompat.getDrawable(context, R.drawable.border_circle)!!
      }

      override fun setNameSelection(selection: Boolean) {
      if (parentPosition == 6) {
      if (position == 1) {
      tShirtBean.isSelected = false
      }
      }
      }






      android kotlin interface






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 26 '18 at 7:08







      Farhana

















      asked Nov 26 '18 at 6:28









      FarhanaFarhana

      2,68552335




      2,68552335
























          1 Answer
          1






          active

          oldest

          votes


















          2














          This line selection=RowSubTShirtViewModel references the view model as if it were a named object, meaning you would have written instead of class object:



          object RowSubTShirtViewModel {
          //...
          }


          However, since that's not the case, kotlin is telling you that you cannot reference it like that and must initialize it. The conductor as quite a lot of parameters for me to guess what they are, but essentially you'd have to pass them in:



          selection=RowSubTShirtViewModel(/*parameters here*/)





          share|improve this answer
























          • I got your point. but other problems have to face, Actually this RowSubTShirtViewModel is a viewmodel of subAdapter class and it is using in ViewModel class of Activity and there is a dialog in activity, in dialog on button click I want to reverse selection of RecycleView item and the RowSubTShirtViewModel is a viewModel for the adpater of that recyclerview, so how can i manage constructor of RowSubTShirtViewModel in Acitivty

            – Farhana
            Nov 26 '18 at 7:29








          • 1





            That's just a classic dependency problem. You just carry on passing everything until you have all you need. For example, if you construct RowSubTShirtViewModel in the adapter, which is used in an activity, then you can pass the activity as you instantiate the adapter and then pass it in as you instantiate RowSubTShirtViewModel. There are other approaches using dependency injection frameworks like Dagger. I can't really tell you what suits you best, but the problem you're having will always lead to having to build RowSubTShirtViewModel.

            – Fred
            Nov 26 '18 at 9:09











          • Thanks Fred I will look both the solutions.

            – Farhana
            Nov 26 '18 at 9:11











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53475759%2fclassifier-does-not-have-a-companion-object-and-thus-must-be-initialized-here%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          This line selection=RowSubTShirtViewModel references the view model as if it were a named object, meaning you would have written instead of class object:



          object RowSubTShirtViewModel {
          //...
          }


          However, since that's not the case, kotlin is telling you that you cannot reference it like that and must initialize it. The conductor as quite a lot of parameters for me to guess what they are, but essentially you'd have to pass them in:



          selection=RowSubTShirtViewModel(/*parameters here*/)





          share|improve this answer
























          • I got your point. but other problems have to face, Actually this RowSubTShirtViewModel is a viewmodel of subAdapter class and it is using in ViewModel class of Activity and there is a dialog in activity, in dialog on button click I want to reverse selection of RecycleView item and the RowSubTShirtViewModel is a viewModel for the adpater of that recyclerview, so how can i manage constructor of RowSubTShirtViewModel in Acitivty

            – Farhana
            Nov 26 '18 at 7:29








          • 1





            That's just a classic dependency problem. You just carry on passing everything until you have all you need. For example, if you construct RowSubTShirtViewModel in the adapter, which is used in an activity, then you can pass the activity as you instantiate the adapter and then pass it in as you instantiate RowSubTShirtViewModel. There are other approaches using dependency injection frameworks like Dagger. I can't really tell you what suits you best, but the problem you're having will always lead to having to build RowSubTShirtViewModel.

            – Fred
            Nov 26 '18 at 9:09











          • Thanks Fred I will look both the solutions.

            – Farhana
            Nov 26 '18 at 9:11
















          2














          This line selection=RowSubTShirtViewModel references the view model as if it were a named object, meaning you would have written instead of class object:



          object RowSubTShirtViewModel {
          //...
          }


          However, since that's not the case, kotlin is telling you that you cannot reference it like that and must initialize it. The conductor as quite a lot of parameters for me to guess what they are, but essentially you'd have to pass them in:



          selection=RowSubTShirtViewModel(/*parameters here*/)





          share|improve this answer
























          • I got your point. but other problems have to face, Actually this RowSubTShirtViewModel is a viewmodel of subAdapter class and it is using in ViewModel class of Activity and there is a dialog in activity, in dialog on button click I want to reverse selection of RecycleView item and the RowSubTShirtViewModel is a viewModel for the adpater of that recyclerview, so how can i manage constructor of RowSubTShirtViewModel in Acitivty

            – Farhana
            Nov 26 '18 at 7:29








          • 1





            That's just a classic dependency problem. You just carry on passing everything until you have all you need. For example, if you construct RowSubTShirtViewModel in the adapter, which is used in an activity, then you can pass the activity as you instantiate the adapter and then pass it in as you instantiate RowSubTShirtViewModel. There are other approaches using dependency injection frameworks like Dagger. I can't really tell you what suits you best, but the problem you're having will always lead to having to build RowSubTShirtViewModel.

            – Fred
            Nov 26 '18 at 9:09











          • Thanks Fred I will look both the solutions.

            – Farhana
            Nov 26 '18 at 9:11














          2












          2








          2







          This line selection=RowSubTShirtViewModel references the view model as if it were a named object, meaning you would have written instead of class object:



          object RowSubTShirtViewModel {
          //...
          }


          However, since that's not the case, kotlin is telling you that you cannot reference it like that and must initialize it. The conductor as quite a lot of parameters for me to guess what they are, but essentially you'd have to pass them in:



          selection=RowSubTShirtViewModel(/*parameters here*/)





          share|improve this answer













          This line selection=RowSubTShirtViewModel references the view model as if it were a named object, meaning you would have written instead of class object:



          object RowSubTShirtViewModel {
          //...
          }


          However, since that's not the case, kotlin is telling you that you cannot reference it like that and must initialize it. The conductor as quite a lot of parameters for me to guess what they are, but essentially you'd have to pass them in:



          selection=RowSubTShirtViewModel(/*parameters here*/)






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 26 '18 at 7:21









          FredFred

          8,79012645




          8,79012645













          • I got your point. but other problems have to face, Actually this RowSubTShirtViewModel is a viewmodel of subAdapter class and it is using in ViewModel class of Activity and there is a dialog in activity, in dialog on button click I want to reverse selection of RecycleView item and the RowSubTShirtViewModel is a viewModel for the adpater of that recyclerview, so how can i manage constructor of RowSubTShirtViewModel in Acitivty

            – Farhana
            Nov 26 '18 at 7:29








          • 1





            That's just a classic dependency problem. You just carry on passing everything until you have all you need. For example, if you construct RowSubTShirtViewModel in the adapter, which is used in an activity, then you can pass the activity as you instantiate the adapter and then pass it in as you instantiate RowSubTShirtViewModel. There are other approaches using dependency injection frameworks like Dagger. I can't really tell you what suits you best, but the problem you're having will always lead to having to build RowSubTShirtViewModel.

            – Fred
            Nov 26 '18 at 9:09











          • Thanks Fred I will look both the solutions.

            – Farhana
            Nov 26 '18 at 9:11



















          • I got your point. but other problems have to face, Actually this RowSubTShirtViewModel is a viewmodel of subAdapter class and it is using in ViewModel class of Activity and there is a dialog in activity, in dialog on button click I want to reverse selection of RecycleView item and the RowSubTShirtViewModel is a viewModel for the adpater of that recyclerview, so how can i manage constructor of RowSubTShirtViewModel in Acitivty

            – Farhana
            Nov 26 '18 at 7:29








          • 1





            That's just a classic dependency problem. You just carry on passing everything until you have all you need. For example, if you construct RowSubTShirtViewModel in the adapter, which is used in an activity, then you can pass the activity as you instantiate the adapter and then pass it in as you instantiate RowSubTShirtViewModel. There are other approaches using dependency injection frameworks like Dagger. I can't really tell you what suits you best, but the problem you're having will always lead to having to build RowSubTShirtViewModel.

            – Fred
            Nov 26 '18 at 9:09











          • Thanks Fred I will look both the solutions.

            – Farhana
            Nov 26 '18 at 9:11

















          I got your point. but other problems have to face, Actually this RowSubTShirtViewModel is a viewmodel of subAdapter class and it is using in ViewModel class of Activity and there is a dialog in activity, in dialog on button click I want to reverse selection of RecycleView item and the RowSubTShirtViewModel is a viewModel for the adpater of that recyclerview, so how can i manage constructor of RowSubTShirtViewModel in Acitivty

          – Farhana
          Nov 26 '18 at 7:29







          I got your point. but other problems have to face, Actually this RowSubTShirtViewModel is a viewmodel of subAdapter class and it is using in ViewModel class of Activity and there is a dialog in activity, in dialog on button click I want to reverse selection of RecycleView item and the RowSubTShirtViewModel is a viewModel for the adpater of that recyclerview, so how can i manage constructor of RowSubTShirtViewModel in Acitivty

          – Farhana
          Nov 26 '18 at 7:29






          1




          1





          That's just a classic dependency problem. You just carry on passing everything until you have all you need. For example, if you construct RowSubTShirtViewModel in the adapter, which is used in an activity, then you can pass the activity as you instantiate the adapter and then pass it in as you instantiate RowSubTShirtViewModel. There are other approaches using dependency injection frameworks like Dagger. I can't really tell you what suits you best, but the problem you're having will always lead to having to build RowSubTShirtViewModel.

          – Fred
          Nov 26 '18 at 9:09





          That's just a classic dependency problem. You just carry on passing everything until you have all you need. For example, if you construct RowSubTShirtViewModel in the adapter, which is used in an activity, then you can pass the activity as you instantiate the adapter and then pass it in as you instantiate RowSubTShirtViewModel. There are other approaches using dependency injection frameworks like Dagger. I can't really tell you what suits you best, but the problem you're having will always lead to having to build RowSubTShirtViewModel.

          – Fred
          Nov 26 '18 at 9:09













          Thanks Fred I will look both the solutions.

          – Farhana
          Nov 26 '18 at 9:11





          Thanks Fred I will look both the solutions.

          – Farhana
          Nov 26 '18 at 9:11




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53475759%2fclassifier-does-not-have-a-companion-object-and-thus-must-be-initialized-here%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Costa Masnaga

          Fotorealismo

          Sidney Franklin