KhanStan99
KhanStan99

Reputation: 424

How to assign a single object to a string variable from selected spinner item

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

Answers (3)

Sathiesh Palanisamy
Sathiesh Palanisamy

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

PRIYA PARASHAR
PRIYA PARASHAR

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

Vinayak B
Vinayak B

Reputation: 4520

try below code

strNationality = (String) countrySpinner.getSelectedItem();
String [] split= strNationality.split("-");
strNationality = split[1];

Upvotes: 1

Related Questions