Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/220.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
微调器只选择了一次nedded,Android Studio_Android - Fatal编程技术网

微调器只选择了一次nedded,Android Studio

微调器只选择了一次nedded,Android Studio,android,Android,我试图使用工具栏中的微调器实现语言更改。现在,我为微调器提供了以下代码: initList(); Spinner spinnerCountries = findViewById(R.id.spinner_limba); mAdapter = new SpinnerAdapterTara(Harta.this, mCountryList); spinnerCountries.setAdapter(mAdapter); spinnerCountries.setOnItemSelect

我试图使用
工具栏中的
微调器实现语言更改。现在,我为
微调器提供了以下代码:

initList();
Spinner spinnerCountries = findViewById(R.id.spinner_limba);
mAdapter = new SpinnerAdapterTara(Harta.this, mCountryList);
    spinnerCountries.setAdapter(mAdapter);
    spinnerCountries.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        TaraITEM clickedItem = (TaraITEM) parent.getItemAtPosition(position);
        String clickedCountryName = clickedItem.getCountryName();
        if (clickedCountryName.equals("FR")){
            setLocale("fr");
            recreate();
        } else if (clickedCountryName.equals("EN")){
            setLocale("en");
            recreate();
        }
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }
});
编辑:添加了
initList()

private void initList(){
mCountryList=新的ArrayList();
McCountryList.add(新项目(“RO”,R.drawable.steag_RO));
mCountryList.add(新项目(“EN”,R.drawable.steag_EN));
mCountryList.add(新项目(“FR”,R.drawable.steag_FR));
}

使用
静态
变量保存最后一个
选择
,并在重新创建
活动
之前检查该变量,该活动会给您带来类似
onChangeListener

//Place inside Harta like global variable
static String lastCountryName = "";

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    TaraITEM clickedItem = (TaraITEM) parent.getItemAtPosition(position);
    String clickedCountryName = clickedItem.getCountryName();

    // compare here before recreating
    if(!clickedCountryName.equals(lastCountryName)) {
        lastCountryName = clickedCountryName;

        if (clickedCountryName.equals("FR")){
            setLocale("fr");
            recreate();
        } else if (clickedCountryName.equals("EN")){
            setLocale("en");
            recreate();
        }
    }
}
//放入类似Harta的全局变量中
静态字符串lastCountryName=“”;
@凌驾
已选择公共视图(AdapterView父视图、视图视图、整型位置、长id){
TaraITEM clickedItem=(TaraITEM)父项.getItemAtPosition(position);
字符串clickedCountryName=clickedItem.getCountryName();
//在重新创建之前在此进行比较
如果(!clickedCountryName.equals(lastCountryName)){
lastCountryName=单击的CountryName;
如果(单击CountryName.equals(“FR”)){
setLocale(“fr”);
重新创建();
}否则如果(单击CountryName.equals(“EN”)){
设置语言环境(“en”);
重新创建();
}
}
}

使用
静态
变量保存最后一个
选择
,并在重新创建
活动
之前检查该变量,该活动会给您带来类似
onChangeListener

//Place inside Harta like global variable
static String lastCountryName = "";

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    TaraITEM clickedItem = (TaraITEM) parent.getItemAtPosition(position);
    String clickedCountryName = clickedItem.getCountryName();

    // compare here before recreating
    if(!clickedCountryName.equals(lastCountryName)) {
        lastCountryName = clickedCountryName;

        if (clickedCountryName.equals("FR")){
            setLocale("fr");
            recreate();
        } else if (clickedCountryName.equals("EN")){
            setLocale("en");
            recreate();
        }
    }
}
//放入类似Harta的全局变量中
静态字符串lastCountryName=“”;
@凌驾
已选择公共视图(AdapterView父视图、视图视图、整型位置、长id){
TaraITEM clickedItem=(TaraITEM)父项.getItemAtPosition(position);
字符串clickedCountryName=clickedItem.getCountryName();
//在重新创建之前在此进行比较
如果(!clickedCountryName.equals(lastCountryName)){
lastCountryName=单击的CountryName;
如果(单击CountryName.equals(“FR”)){
setLocale(“fr”);
重新创建();
}否则如果(单击CountryName.equals(“EN”)){
设置语言环境(“en”);
重新创建();
}
}
}

这是否回答了您的问题?你可以看看这个答案。该答案/问题对我的情况没有帮助。当用户在设置一次区域设置后可以重置区域设置时?您可以添加
initList()
实现吗?这是否回答了您的问题?你可以看看这个答案。这个答案/问题对我的情况没有帮助。当用户在设置一次区域设置后可以重置区域设置时,你能添加你的
initList()
实现吗?效果很好。现在我需要找到为什么它没有切换到
EN
,而只切换到
FR
就像一个符咒。现在我需要找到为什么它没有切换到
EN
,而只切换到
FR
//Place inside Harta like global variable
static String lastCountryName = "";

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    TaraITEM clickedItem = (TaraITEM) parent.getItemAtPosition(position);
    String clickedCountryName = clickedItem.getCountryName();

    // compare here before recreating
    if(!clickedCountryName.equals(lastCountryName)) {
        lastCountryName = clickedCountryName;

        if (clickedCountryName.equals("FR")){
            setLocale("fr");
            recreate();
        } else if (clickedCountryName.equals("EN")){
            setLocale("en");
            recreate();
        }
    }
}