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;
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
add a comment |
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
You can createDatabaseModuleclass 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
add a comment |
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
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
java
asked Mar 23 at 10:57
ip696ip696
1,39621543
1,39621543
You can createDatabaseModuleclass 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
add a comment |
You can createDatabaseModuleclass 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
add a comment |
1 Answer
1
active
oldest
votes
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);
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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);
add a comment |
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);
add a comment |
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);
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);
answered Apr 17 at 7:33
ninad thakareninad thakare
613
613
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
You can create
DatabaseModuleclass 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