【问题标题】:Cannot read country code selected in a spinner Android无法读取在微调器 Android 中选择的国家/地区代码
【发布时间】:2020-09-14 16:34:03
【问题描述】:

我正在开发一个用户必须选择国家代码的应用程序,我成功地为上述目的创建了一个微调器,如以下链接所示:
Creating a spinner for choosing country code 但我在读取微调器中选择的值时遇到问题。

{
               String abc = onCountryPickerClick();//abc is always null 
}  
    public String onCountryPickerClick (){
         ccp.setOnCountryChangeListener(new CountryCodePicker.OnCountryChangeListener() {
             @Override
             public void onCountrySelected() {
                 selected_country_code = ccp.getSelectedCountryCodeWithPlus();
             }

         });
         return  selected_country_code;
    }

【问题讨论】:

  • 用代码编辑你的问题
  • 你看了上面的链接问题,你试过上面的链接答案了吗?

标签: android android-studio spinner mobile-country-code


【解决方案1】:

String abc = onCountryPickerClick();被调用时,selected_country_code的值将被赋值给abc

当您的CountryCodePicker.OnCountryChangeListeneronCountrySelected() 方法被调用时,ccp.getSelectedCountryCodeWithPlus(); 的值被分配给selected_country_code。由于String 是不可变的,因此更改selected_country_code 的值不会更改abc 的值,也不会调用return selected_country_code;

一种可能的解决方案是更改您的 CountryCodePicker.OnCountryChangeListener 匿名实现以将所选国家/地区值分配给 abc 例如

@Override
public void onCountrySelected() {
       selected_country_code = ccp.getSelectedCountryCodeWithPlus();
       abc = selected_country_code
}

【讨论】:

  • 非常感谢它的工作。只需要设置 abc =selected_country_code;然后返回 abc 而不是返回 selected_country_code。
【解决方案2】:

回调不是同步的。不幸的是,您不能简单地执行String abc = onCountryPickerClick();,因为您返回的是尚未设置的内容。让我们看看你的代码:

ccp.setOnCountryChangeListener(
     new CountryCodePicker.OnCountryChangeListener() {
         @Override
         public void onCountrySelected() {
             selected_country_code = ccp.getSelectedCountryCodeWithPlus();
         }
     });

代码似乎是说,当在微调器中选择国家时,您分配selected_country_code 的值。假设这是一个用户触发的动作,当你调用String abc = onCountryPickerClick();,你怎么能确定用户选择了什么?这就是问题所在。您不能确定用户已经选择了该选项并返回该值是不够的。

您可以通过多种方式解决此问题。例如,您可以继续传播回调:

public void onCountryPickerClick(OnCountryChangeListener listener){
     ccp.setOnCountryChangeListener(listener);
}

// Anywhere you call this
onCountryPickerClick(new CountryCodePicker.OnCountryChangeListener() {
         @Override
         public void onCountrySelected() {
             // Here do whatever you want with the selected country
         }
     });

上述方法与您现在的方法没有太大区别。还有其他选择。您可以使用 java observables,即:

class CountryCodeObservable extends Observable {
  private String value;

  public CountryCodeObservable(String value) {
    this.value = value;
  }

  public void setCountryCode(String countryCode) {
    value = countryCode;
    setChanged();
    notifyObservers(value);
  }
}

public CountryCodeObservable onCountryPickerClick(){
  CountryCodeObservable retValue = new CountryCodeObservable("");

  ccp.setOnCountryChangeListener(
     new CountryCodePicker.OnCountryChangeListener() {
         @Override
         public void onCountrySelected() {
             retValue.setCountryCode(ccp.getSelectedCountryCodeWithPlus());
         }
     });

  return retValue;
}

// Then when calling this method you can do something like:
CountryCodeObservable observable = onCountryPickerClick();

observable.addObserver((obj, arg) -> {
   // arg is the value that changed. You'll probably need to cast it to
   // a string
});

上面的例子允许你添加多个 observable。对于您的用例而言,这可能太多了,我只是认为它说明了另一种方法以及这种情况的异步性。

同样,还有更多的方法可以解决这个问题,关键是你不能简单地返回一个字符串并希望当用户选择任何东西时它会改变。

【讨论】:

  • 你是对的,我不确定用户是否选择了任何东西,为了避免这种情况,我在变量 abc 中设置了一个默认值,这意味着如果用户没有选择任何东西,默认值为自动选择,如果他这样做,现有值将被新值替换。
  • 我明白,但没有任何东西告诉您的应用程序的值发生了变化。这就是我的意思,你不能确定用户选择了任何东西。当您从您的方法返回 String 时,该字符串没有在用户输入时更新的机制。要实现这一点,您需要类似于我所写的内容。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多