0

So, I have a database that returns 2 integer-type values (1 = USD and 3 = GHS). I wrote an API PHP code that returns the integer values and would like to convert their corresponding String (USD and GHS) in Java.

The line of code that returns the int in java (android studio) is

creditnav.setText(SharedPrefManager.getInstance(MainActivity.this).getUser().getCurrency());

I would like to return the result of the above line of code into their corresponding strings.

How do I go about it? Any help would be much appreciated.

Taher A. Ghaleb
  • 5,120
  • 5
  • 31
  • 44
  • Possible duplicate of [How do I convert from int to String?](https://stackoverflow.com/questions/4105331/how-do-i-convert-from-int-to-string) – Geshode Dec 03 '18 at 03:50
  • Please do provide enough detail so that we can help you. – Abhinav Dec 03 '18 at 03:53

3 Answers3

1

Try this

int currencyCode = SharedPrefManager.getInstance(MainActivity.this).getUser().getCurrency();
if (currencyCode == 1) {
    creditnav.setText("USD");
}
if (currencyCode == 3) {
    creditnav.setText("GHS");
}
Erwin Kurniawan A
  • 958
  • 2
  • 9
  • 17
  • Thanks soo much. This solved my issue. `code` int currencyCode = SharedPrefManager.getInstance(getActivity()).getUser().getCurrency(); if (currencyCode == 1) { credit.setText("$"+ " " + SharedPrefManager.getInstance(getActivity()).getUser().getCredit()); currency.setText("USD"); } if (currencyCode == 3) { credit.setText("GHS"+ " " + SharedPrefManager.getInstance(getActivity()).getUser().getCredit()); currency.setText("GHS"); } – Gideon Koney Dec 04 '18 at 15:41
0

You could edit the getCurrency() and add a switch/if else statement and return a string, or create another method getCurrencyString()

switch (currencyInteger) {
  case 1 :
      return "USD";

  case 2 :
      return "GHS";

  default:
      // Handle this
      break;
}
  • how would a switch statement like this be implemented using the sharedpreference code i shared. Can you write the full code using the sharedpreference code? – Gideon Koney Dec 04 '18 at 15:45
0

try to use this one if it may help you :).

 String creds=SharedPrefManager.getInstance(MainActivity.this).getUser().getCurrency(); 


if (creds.getText().toString.equals("1")) {
  creditnav.setText("USD");
}
else if (creds.getText().toString.equals("3")) {
   creditnav.setText("GHS");

}

PAkultie
  • 102
  • 1
  • 11