How can I create ViewModel containing DAO object in Dager 2?How can I concatenate two arrays in Java?How do I create a Java string from the contents of a file?How can I create an executable JAR with dependencies using Maven?How do I determine whether an array contains a particular value in Java?How can I convert a stack trace to a string?Why is the Android emulator so slow? How can we speed up the Android emulator?How do I create a file and write to it in Java?Unresolved inject method on Dagger2 methodWhat is the point of injecting a ViewModelFactory in Android - Dagger 2Dagger 2 multiple Repositories

What's the best way to update Homebrew when upgrading macOS?

Is a vertical stabiliser needed for straight line flight in a glider?

Can you book a one-way ticket to the UK on a visa?

Why do Thanos's punches not kill Captain America or at least cause some mortal injuries?

Drawing perpendicular lines, filling areas

How can this pool heater gas line be disconnected?

How can I answer high-school writing prompts without sounding weird and fake?

Should these notes be played as a chord or one after another?

Why do unstable nuclei form?

How does Howard Stark know this?

Understanding basic photoresistor circuit

Guns in space with bullets that return?

On studying Computer Science vs. Software Engineering to become a proficient coder

Looking for a simple way to manipulate one column of a matrix

Is there a need for better software for writers?

Increase height of laser cut design file for enclosure

Hiker’s Cabin Mystery | Pt. IV

"Right on the tip of my tongue" meaning?

What is the best way for a skeleton to impersonate human without using magic?

Why in a Ethernet LAN, a packet sniffer can obtain all packets sent over the LAN?

Why was this sacrifice sufficient?

Why does a C.D.F need to be right-continuous?

How to pronounce "r" after a "g"?

The lexical root of the perfect tense forms differs from the lexical root of the infinitive form



How can I create ViewModel containing DAO object in Dager 2?


How can I concatenate two arrays in Java?How do I create a Java string from the contents of a file?How can I create an executable JAR with dependencies using Maven?How do I determine whether an array contains a particular value in Java?How can I convert a stack trace to a string?Why is the Android emulator so slow? How can we speed up the Android emulator?How do I create a file and write to it in Java?Unresolved inject method on Dagger2 methodWhat is the point of injecting a ViewModelFactory in Android - Dagger 2Dagger 2 multiple Repositories






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I create android ViewModel in Activity:



EventViewModel model = ViewModelProviders.of(this).get(EventViewModel.class);


For this I create EventViewModel :



public class EventViewModel extends ViewModel 

private final EventDao eventDao;

public EventViewModel(EventDao eventDao)
this.eventDao = eventDao;


public void createEvent(final Event event)
new Thread(new Runnable()
@Override
public void run()
eventDao.insert(event);

).start();




I learned that I must create custom factory in order to inject my EventDao to EventViewModel. Ok, let's say i did it.



public class ViewModelFactory implements ViewModelProvider.Factory 

private final EventDao eventDao;

@Inject
public ViewModelFactory(EventDao eventDao)
this.eventDao = eventDao;



@NonNull
@Override
public <T extends ViewModel> T create(@NonNull Class<T> modelClass)
if (modelClass == EventViewModel.class)
return (T) new EventViewModel(eventDao);

return null;




but what to do next? I see several ways. If belive to examples from google I must do next in MyActivity:



EventViewModel model = ViewModelProviders.of(this, new ModelFactory(eventDao)).get(EventViewModel.class);


1) But where do i get eventDao in MyActivity?



2) Do I need create custom ModelFactory for each ViewModel if it use dao class in?



I use Dagger 2 and I just want understand how can I create ViewModel with DAO and use this ViewModel in MyActivity?










share|improve this question






















  • You can create DatabaseModule class and provide dao from that class. So You just need to @Injet the Dao in your activity

    – Pallavi Tapkir
    Mar 25 at 13:12












  • Please add the code for your Component which you are trying to configure with @Inject

    – EpicPandaForce
    Mar 25 at 21:46

















0















I create android ViewModel in Activity:



EventViewModel model = ViewModelProviders.of(this).get(EventViewModel.class);


For this I create EventViewModel :



public class EventViewModel extends ViewModel 

private final EventDao eventDao;

public EventViewModel(EventDao eventDao)
this.eventDao = eventDao;


public void createEvent(final Event event)
new Thread(new Runnable()
@Override
public void run()
eventDao.insert(event);

).start();




I learned that I must create custom factory in order to inject my EventDao to EventViewModel. Ok, let's say i did it.



public class ViewModelFactory implements ViewModelProvider.Factory 

private final EventDao eventDao;

@Inject
public ViewModelFactory(EventDao eventDao)
this.eventDao = eventDao;



@NonNull
@Override
public <T extends ViewModel> T create(@NonNull Class<T> modelClass)
if (modelClass == EventViewModel.class)
return (T) new EventViewModel(eventDao);

return null;




but what to do next? I see several ways. If belive to examples from google I must do next in MyActivity:



EventViewModel model = ViewModelProviders.of(this, new ModelFactory(eventDao)).get(EventViewModel.class);


1) But where do i get eventDao in MyActivity?



2) Do I need create custom ModelFactory for each ViewModel if it use dao class in?



I use Dagger 2 and I just want understand how can I create ViewModel with DAO and use this ViewModel in MyActivity?










share|improve this question






















  • You can create DatabaseModule class and provide dao from that class. So You just need to @Injet the Dao in your activity

    – Pallavi Tapkir
    Mar 25 at 13:12












  • Please add the code for your Component which you are trying to configure with @Inject

    – EpicPandaForce
    Mar 25 at 21:46













0












0








0








I create android ViewModel in Activity:



EventViewModel model = ViewModelProviders.of(this).get(EventViewModel.class);


For this I create EventViewModel :



public class EventViewModel extends ViewModel 

private final EventDao eventDao;

public EventViewModel(EventDao eventDao)
this.eventDao = eventDao;


public void createEvent(final Event event)
new Thread(new Runnable()
@Override
public void run()
eventDao.insert(event);

).start();




I learned that I must create custom factory in order to inject my EventDao to EventViewModel. Ok, let's say i did it.



public class ViewModelFactory implements ViewModelProvider.Factory 

private final EventDao eventDao;

@Inject
public ViewModelFactory(EventDao eventDao)
this.eventDao = eventDao;



@NonNull
@Override
public <T extends ViewModel> T create(@NonNull Class<T> modelClass)
if (modelClass == EventViewModel.class)
return (T) new EventViewModel(eventDao);

return null;




but what to do next? I see several ways. If belive to examples from google I must do next in MyActivity:



EventViewModel model = ViewModelProviders.of(this, new ModelFactory(eventDao)).get(EventViewModel.class);


1) But where do i get eventDao in MyActivity?



2) Do I need create custom ModelFactory for each ViewModel if it use dao class in?



I use Dagger 2 and I just want understand how can I create ViewModel with DAO and use this ViewModel in MyActivity?










share|improve this question














I create android ViewModel in Activity:



EventViewModel model = ViewModelProviders.of(this).get(EventViewModel.class);


For this I create EventViewModel :



public class EventViewModel extends ViewModel 

private final EventDao eventDao;

public EventViewModel(EventDao eventDao)
this.eventDao = eventDao;


public void createEvent(final Event event)
new Thread(new Runnable()
@Override
public void run()
eventDao.insert(event);

).start();




I learned that I must create custom factory in order to inject my EventDao to EventViewModel. Ok, let's say i did it.



public class ViewModelFactory implements ViewModelProvider.Factory 

private final EventDao eventDao;

@Inject
public ViewModelFactory(EventDao eventDao)
this.eventDao = eventDao;



@NonNull
@Override
public <T extends ViewModel> T create(@NonNull Class<T> modelClass)
if (modelClass == EventViewModel.class)
return (T) new EventViewModel(eventDao);

return null;




but what to do next? I see several ways. If belive to examples from google I must do next in MyActivity:



EventViewModel model = ViewModelProviders.of(this, new ModelFactory(eventDao)).get(EventViewModel.class);


1) But where do i get eventDao in MyActivity?



2) Do I need create custom ModelFactory for each ViewModel if it use dao class in?



I use Dagger 2 and I just want understand how can I create ViewModel with DAO and use this ViewModel in MyActivity?







java android mvvm dagger-2






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 23 at 10:57









ip696ip696

1,39621543




1,39621543












  • You can create DatabaseModule class and provide dao from that class. So You just need to @Injet the Dao in your activity

    – Pallavi Tapkir
    Mar 25 at 13:12












  • Please add the code for your Component which you are trying to configure with @Inject

    – EpicPandaForce
    Mar 25 at 21:46

















  • You can create DatabaseModule class and provide dao from that class. So You just need to @Injet the Dao in your activity

    – Pallavi Tapkir
    Mar 25 at 13:12












  • Please add the code for your Component which you are trying to configure with @Inject

    – EpicPandaForce
    Mar 25 at 21:46
















You can create DatabaseModule class and provide dao from that class. So You just need to @Injet the Dao in your activity

– Pallavi Tapkir
Mar 25 at 13:12






You can create DatabaseModule class and provide dao from that class. So You just need to @Injet the Dao in your activity

– Pallavi Tapkir
Mar 25 at 13:12














Please add the code for your Component which you are trying to configure with @Inject

– EpicPandaForce
Mar 25 at 21:46





Please add the code for your Component which you are trying to configure with @Inject

– EpicPandaForce
Mar 25 at 21:46












1 Answer
1






active

oldest

votes


















0














You inject ViewModelFactory in the activity. ViewModelFactory will get EventDao through constructor injection. You pass the injected ViewModelFactory instance say viewModelFactory to ViewModelProviders



EventViewModel model = ViewModelProviders.of(this, viewModelFactory).get(EventViewModel.class);






share|improve this answer























    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%2f55312980%2fhow-can-i-create-viewmodel-containing-dao-object-in-dager-2%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









    0














    You inject ViewModelFactory in the activity. ViewModelFactory will get EventDao through constructor injection. You pass the injected ViewModelFactory instance say viewModelFactory to ViewModelProviders



    EventViewModel model = ViewModelProviders.of(this, viewModelFactory).get(EventViewModel.class);






    share|improve this answer



























      0














      You inject ViewModelFactory in the activity. ViewModelFactory will get EventDao through constructor injection. You pass the injected ViewModelFactory instance say viewModelFactory to ViewModelProviders



      EventViewModel model = ViewModelProviders.of(this, viewModelFactory).get(EventViewModel.class);






      share|improve this answer

























        0












        0








        0







        You inject ViewModelFactory in the activity. ViewModelFactory will get EventDao through constructor injection. You pass the injected ViewModelFactory instance say viewModelFactory to ViewModelProviders



        EventViewModel model = ViewModelProviders.of(this, viewModelFactory).get(EventViewModel.class);






        share|improve this answer













        You inject ViewModelFactory in the activity. ViewModelFactory will get EventDao through constructor injection. You pass the injected ViewModelFactory instance say viewModelFactory to ViewModelProviders



        EventViewModel model = ViewModelProviders.of(this, viewModelFactory).get(EventViewModel.class);







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Apr 17 at 7:33









        ninad thakareninad thakare

        613




        613





























            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%2f55312980%2fhow-can-i-create-viewmodel-containing-dao-object-in-dager-2%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

            SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

            용인 삼성생명 블루밍스 목차 통계 역대 감독 선수단 응원단 경기장 같이 보기 외부 링크 둘러보기 메뉴samsungblueminx.comeh선수 명단용인 삼성생명 블루밍스용인 삼성생명 블루밍스ehsamsungblueminx.comeheheheh

            155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해