Reputation: 424
I want to get countryCode
of selected country from my spinner.
here is the ArrayList
countryList.add(jsonObjectHoldingCountries.optString("countryName") + " - " + jsonObjectHoldingCountries.optString("countryCode"));
now i want to get only countryCode
but it is returning me both countryName
and countryCode
I tried it like this:
strNationality = (String) countrySpinner.getSelectedItem();
but now strNationality
will have both country code and country name.
If you can help me it will be a great help! :)
Upvotes: 1
Views: 52
Reputation: 59
If you want to print directly use the below code
String str[] = strNationality.split("-");
System.out.println(str[1]);
If you want to access this value to multiple place use the following methods
String couCode = null;
String str[] = strNationality.split("-");
couCode = str[1];
System.out.println(couCode);
Now use couCode to anywhere to access based on couCode declaration ie., either globally or locally
Upvotes: 0
Reputation: 777
You can splIt the strNationality like this:
strNationality = (String) countrySpinner.getSelectedItem();
String data[]=strNationality.split("-");
if(data.length==2)
{
String countryName=data[0];
String countryCode=data[1];}
Upvotes: 2
Reputation: 4520
try below code
strNationality = (String) countrySpinner.getSelectedItem();
String [] split= strNationality.split("-");
strNationality = split[1];
Upvotes: 1