代码之家  ›  专栏  ›  技术社区  ›  KhanStan99

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

  •  1
  • KhanStan99  · 技术社区  · 8 年前

    我想得到 countryCode

    这是ArrayList

    countryList.add(jsonObjectHoldingCountries.optString("countryName") + " - " + jsonObjectHoldingCountries.optString("countryCode"));
    

    现在我只想得到 国家代码 countryName

    我这样试过:

    strNationality = (String) countrySpinner.getSelectedItem();
    

    但是现在 strNationality 将包含国家代码和国家名称。

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

    3 回复  |  直到 8 年前
        1
  •  2
  •   PRIYA PARASHAR    8 年前

    你可以这样分割结构:

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

    尝试以下代码

    strNationality = (String) countrySpinner.getSelectedItem();
    String [] split= strNationality.split("-");
    strNationality = split[1];
    
        3
  •  0
  •   Sathiesh Palanisamy    8 年前

    如果要直接打印,请使用以下代码

       String str[] = strNationality.split("-");
       System.out.println(str[1]);
    

    如果要在多个位置访问此值,请使用以下方法

       String couCode = null; 
       String str[] = strNationality.split("-");
       couCode = str[1];
       System.out.println(couCode);
    

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