Accessing strings.xml from ViewModel

﹥>﹥吖頭↗ 提交于 2019-12-06 02:59:55

There is an AndroidViewModel, which receives Application instance as parameter.

From docs:

Application context aware ViewModel.

Subclasses must have a constructor which accepts Application as the only parameter.

You can retrieve a string from strings.xml using that parameter.


The repo in the link, however uses ViewModel and not AndroidViewModel. If I extend my ViewModel to use AndroidViewModel and include the Application - it's trying to inject MyApplication instead of Application if that makes sense.

I've checked out GithubBrowserSample. Here's how UserViewModel looks like:


    public class UserViewModel extends ViewModel {
        ...
        @Inject
        public UserViewModel(UserRepository userRepository, RepoRepository repoRepository) {
            ...
        }
        ...
    }

And here's what changes I've done:


    public class UserViewModel extends AndroidViewModel {
        ...
        @Inject
        public UserViewModel(Application application, UserRepository userRepository, RepoRepository repoRepository) {
            super(application);
            ...
        }
        ...
    }

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!