1

I've tried many ways for "getString" in the ViewModel , as I've succeeded to getString at the MainActivity.class which the code is just getString(R.string.value) like that ... but I am clueless for getString in ViewModel , I have tried many ways such as using context, Resources.getResouces().getString(R.string.value) etc , please help me and thanks in advance

private int API_KEY = R.string.API_KEY;


Log.d(TAG, "getData: " + context.getString(API_KEY));

I expect the results is the string from strings.xml that could be get from the int of R.string.API_KEY

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • 1
    I hope this is what you need: [How should I get Resources(R.string) in viewModel in Android (MVVM and databinding)](https://stackoverflow.com/questions/30804291/android-access-to-r-string-from-model-class) – Evyatar Cohen Oct 08 '19 at 04:49

2 Answers2

0

You should use getResources

Log.d(TAG, "getData: " + context.getResources().getString(API_KEY));
DinhNguyen
  • 303
  • 2
  • 14
0
  1. ViewModel class should be implemented by AndroidViewModel which has application instance by default.
  2. Then getString can be used by > Example: application.getString(API_KEY)
Roman
  • 2,464
  • 2
  • 17
  • 21