如何将单个对象分配给选定的微调器项目中的字符串变量

时间:2021-03-05 20:52:32

I want to get countryCode of selected country from my spinner.

我想从我的微调器中获取所选国家/地区的countryCode。

here is the ArrayList

这是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

现在我想只获得countryCode,但它返回countryName和countryCode

I tried it like this:

我试过这样的:

strNationality = (String) countrySpinner.getSelectedItem();

but now strNationality will have both country code and country name.

但现在strNationality将同时包含国家代码和国家/地区名称。

If you can help me it will be a great help! :)

如果你能帮助我,那将是一个很大的帮助! :)

3 个解决方案

#1


2  

You can splIt the strNationality like this:

你可以像这样分割strNationality:

  strNationality = (String) countrySpinner.getSelectedItem();
    String data[]=strNationality.split("-");
if(data.length==2)
{
    String countryName=data[0];
    String countryCode=data[1];}

#2


1  

try below code

尝试以下代码

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

#3


0  

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

现在使用couCode到任何地方基于couCode声明访问,即全局或本地访问

#1


2  

You can splIt the strNationality like this:

你可以像这样分割strNationality:

  strNationality = (String) countrySpinner.getSelectedItem();
    String data[]=strNationality.split("-");
if(data.length==2)
{
    String countryName=data[0];
    String countryCode=data[1];}

#2


1  

try below code

尝试以下代码

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

#3


0  

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

现在使用couCode到任何地方基于couCode声明访问,即全局或本地访问