【发布时间】:2016-08-06 08:56:19
【问题描述】:
您好,我想知道如何在整个活动中保持选择的微调器项目,以便如果我从其他活动返回到由微调器组成的活动,它仍然保持选中状态。
我使用的上述语句在此之后已解决,还有另一个问题是,每当我重新启动我的应用程序时,其所选部分仍然保持原样,我想知道我能做什么,以便微调器不显示我之前选择了什么
我有一个微调器代码,我想在所有活动中保持选中它,
daySelection = (Spinner)findViewById(R.id.daypreferance);
//String[] dayName = new String[]{
// "sunday", "monday", "tuesday"
//};
MyClass[] dayName ={
new MyClass("sunday", ""),
new MyClass("monday", "2"),
new MyClass("tuesday", "3")
};
ArrayAdapter<MyClass> adapter = new ArrayAdapter<MyClass>(this , R.layout.spinner_items ,R.id.spinneritem,dayName);
adapter.setDropDownViewResource(R.layout.spinner_items);
hotelSelection.setAdapter(adapter);
daySelection.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
preferDay= parent.getItemAtPosition(position).toString();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
public void onPause() {
super.onPause();
daySelection = (Spinner)findViewById(R.id.daypreferance);
SharedPreferences prefs = getSharedPreferences("prefs_name", Context.MODE_PRIVATE);
prefs.edit().putInt("spinner_indx", daySelection.getSelectedItemPosition()).apply();
}
public void onResume() {
super.onResume();
daySelection = (Spinner)findViewById(R.id.daypreferance);
SharedPreferences prefs = getSharedPreferences("prefs_name", Context.MODE_PRIVATE);
int spinnerIndx = prefs.getInt("spinner_indx", 0);
daySelection.setSelection(spinnerIndx);
}
我需要添加代码,以便每当我重新启动我的应用程序时,其所选部分保持不变我想知道我可以做什么,以便微调器不会显示我之前选择的内容
【问题讨论】:
-
您可以在活动中获取一个静态变量并将其设置为选定的位置,并在 onResume 方法中的同一活动上通过该变量设置微调器选择
-
@Vickyexpert 为什么是静态的?
-
@Moulick 没有完成当前的活动,有微调器只是转到下一个活动,当你完成后,只需完成活动,上一个活动将自动恢复,您的选择不会丢失。跨度>
-
但是如何做到这一点通常在填充数据后我将转到下一个活动以显示其摘要,如果我认为我需要返回,我将返回此活动(视图),它应该显示我之前选择的任何内容