微调器不清除
Spinner not clearing
我有一个奇怪的错误,我不确定为什么会这样,
我有 2 个活动:activity A 和 activity B。
当应用程序启动时,它会初始化一次微调器。我转到第二个 activity 然后返回 <-
onRestart()
的 Activity 会被调用。我的问题是,即使我清除微调器并在 onRestart()
中重新初始化它,它也会继续读取值。
例如,如果当您转到 activity B 然后返回 activity A 时,旋转器有 1/2,则旋转器有 = 1/2/1/2。
我觉得这很奇怪,因为当我将微调器适配器设置为 null 并在我从 activity B 返回到 activity A 时注释掉我的微调器初始化时,我的微调器是空的。
这是我的代码:
@Override public void onRestart() {
super.onRestart();
Spinner location = findViewById(R.id.spinnerLocation);
location.setAdapter(null);
initSpinner();// if this is commented out spinner is cleared, if not commented the same valus are added again
}
这是 initSpinner 的代码(从共享首选项读取)这不是问题所在
public void initSpinner(){
Spinner location = findViewById(R.id.spinnerLocation);
location.setAdapter(null);
SharedPreferences prefs = this.getSharedPreferences("Locations", Context.MODE_PRIVATE); //preffilename
SharedPreferences.Editor editor = prefs.edit();
editor.putString("1","St. Catharines");
editor.putString("2","Toronto");
editor.putString("3","Niagara Falls");
editor.commit();
int s = prefs.getAll().size();
for(int f=0;f<25;f++) {
if (prefs.getString(String.valueOf(f + 1), null) != null) {
DefaultLocations.add(prefs.getString(String.valueOf(f + 1), null));
}
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, DefaultLocations);
//set the spinners adapter to the previously created one.
location.setAdapter(adapter);
}
做
DefaultLocations.clear()
在 initSpinner()
方法中的 for 循环之前 添加新值时可能不清楚
我有一个奇怪的错误,我不确定为什么会这样,
我有 2 个活动:activity A 和 activity B。
当应用程序启动时,它会初始化一次微调器。我转到第二个 activity 然后返回 <-
onRestart()
的 Activity 会被调用。我的问题是,即使我清除微调器并在 onRestart()
中重新初始化它,它也会继续读取值。
例如,如果当您转到 activity B 然后返回 activity A 时,旋转器有 1/2,则旋转器有 = 1/2/1/2。
我觉得这很奇怪,因为当我将微调器适配器设置为 null 并在我从 activity B 返回到 activity A 时注释掉我的微调器初始化时,我的微调器是空的。
这是我的代码:
@Override public void onRestart() {
super.onRestart();
Spinner location = findViewById(R.id.spinnerLocation);
location.setAdapter(null);
initSpinner();// if this is commented out spinner is cleared, if not commented the same valus are added again
}
这是 initSpinner 的代码(从共享首选项读取)这不是问题所在
public void initSpinner(){
Spinner location = findViewById(R.id.spinnerLocation);
location.setAdapter(null);
SharedPreferences prefs = this.getSharedPreferences("Locations", Context.MODE_PRIVATE); //preffilename
SharedPreferences.Editor editor = prefs.edit();
editor.putString("1","St. Catharines");
editor.putString("2","Toronto");
editor.putString("3","Niagara Falls");
editor.commit();
int s = prefs.getAll().size();
for(int f=0;f<25;f++) {
if (prefs.getString(String.valueOf(f + 1), null) != null) {
DefaultLocations.add(prefs.getString(String.valueOf(f + 1), null));
}
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, DefaultLocations);
//set the spinners adapter to the previously created one.
location.setAdapter(adapter);
}
做
DefaultLocations.clear()
在 initSpinner()
方法中的 for 循环之前 添加新值时可能不清楚