android - 微调器不保存最后选择的值
问题描述
我的 android 应用程序中有一个微调器,每当我转到其他片段并返回时,微调器就会转到第一项。我想要的是微调器应该保留该值。下面是我如何设置微调器。
private void setupSpinner(){
roundSpinnerMap = new HashMap<>();
roundSpinnerItems = new ArrayList<>();
//Get the day of the week in order to figure out which rounds and variables are needed.
Calendar c = Calendar.getInstance();
SimpleDateFormat format = new SimpleDateFormat("MM-dd-yyyy", Locale.US);
Date d = null;
try {
d = format.parse(mDay);
} catch (ParseException e) {
e.printStackTrace();
}
c.setTime(d);
int dayOfWeek = c.get(Calendar.DAY_OF_WEEK) - 1;
//If the day is Sunday then dayOfWeek is 7, not 0.
if(dayOfWeek == 0){
dayOfWeek = 7;
}
for (Models.Round r:rounds) {
//For each round get the completed and total amount.
int[] partWhole = models.getCompletedOfTotal(r.getName(), mDay, dayOfWeek, nameText.getText().toString());
String readable = r.getName() +" (Completed " + partWhole[0] + " of " + partWhole[1] + ")";
//If there are none to complete, then do not show/add the option.
if(!readable.contains("of 0")){
roundSpinnerMap.put(readable, r.getName());
roundSpinnerItems.add(readable);
}
}
ArrayAdapter<String> roundAdapter = new ArrayAdapter<>(
getActivity(), R.layout.simple_spinner_item, roundSpinnerItems
);
roundAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
roundSpinner.setAdapter(roundAdapter);
roundSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String readable = roundSpinner.getSelectedItem().toString();
mRound = roundSpinnerMap.get(readable);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
以下是我的 onCreate 方法:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
models = new ModelsHandler(getActivity());
if (getArguments() != null) {
mHome = getArguments().getBoolean(ARG_HOME);
mUser = getArguments().getString(ARG_USER);
mFacility = getArguments().getString(ARG_FACILITY);
}
calendar = Calendar.getInstance();
mDay = format.format(calendar.getTime());
sharedPreferences = getActivity().getSharedPreferences(MainActivity.PREFERENCES, Context.MODE_PRIVATE);
rounds = models.getAllRounds();
rise = AnimationUtils.loadAnimation(getActivity(), R.anim.rise_vert);
sink = AnimationUtils.loadAnimation(getActivity(), R.anim.sink_vert);
if (mListener == null){
mListener = (OnFragmentInteractionListener) getActivity();
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_splash, container, false);
roundsButton = view.findViewById(R.id.roundsButton);
calendarButton = view.findViewById(R.id.calendarButton);
contactButton = view.findViewById(R.id.contactButton);
syncMainButton = view.findViewById(R.id.syncMainButton);
trendButton = view.findViewById(R.id.trendButton);
prefSyncButton = view.findViewById(R.id.prefSyncButton);
startSyncButton = view.findViewById(R.id.startSyncButton);
contactDevButton = view.findViewById(R.id.contactDevButton);
contactServiceButton = view.findViewById(R.id.contactServiceButton);
roundSpinner = view.findViewById(R.id.roundSpinner);
roundDateText = view.findViewById(R.id.roundDateText);
errorRoundText = view.findViewById(R.id.roundsErrorText);
nameText = view.findViewById(R.id.nameText);
progressBar2 = view.findViewById(R.id.progressBar2);
errorRoundText.setVisibility(View.GONE);
nameText.setText(sharedPreferences.getString(MainActivity.USER_SESSION_KEY,""));
progressBar2.setVisibility(View.GONE);
roundDateText.setText(format.format(calendar.getTime()));
roundDateText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
launchDatePicker();
}
});
contactActionsOpen = false;
syncActionsOpen = false;
if(rounds == null || rounds.isEmpty()){
errorRoundText.setVisibility(View.VISIBLE);
UtilityPole.initDefaultRounds(models);
rounds = models.getAllRounds();
Toast.makeText(getContext(), getResources().getString(R.string.errorRound), Toast.LENGTH_LONG).show();
}
setupSpinner();
initializeAnimationButtons();
initializeButtonActions();
return view;
}
我尝试使用共享首选项,但这不起作用(也许我不知道如何正确使用它,因为我是 android 新手。)。任何帮助,将不胜感激。
解决方案
您将其存储为静态值。自从我使用 android api 以来已经有一段时间了,所以这不会太准确。只需替换正确的方法调用即可
//have a private static field variable
private static int spinnerValue = 0;
private void getSpinner()
{
spinnerValue = spinner.getCurrentValue();
}
private void setSpinner()
{
soinner.setValue(spinnerValue);
}
这是因为static
关键字使变量保留在所有类中,即使该类已通过垃圾回收销毁。
所以这可能是你正在寻找的
推荐阅读
- reactjs - 使用react-router-dom时如何通过路由传递forwardRef
- python-3.x - 如何以pythonic的方式在一个大球体内洗牌?
- javascript - 编码嵌入消息
- sql - 需要帮助在 SAS 中解决这个问题
- amazon-redshift - 在 AWS 上查询 Redshift 集群收到错误“无法连接到查询服务”。
- html - 防止绝对定位的 div 增加其父级的大小
- r - 使用多个变量时更改图例标签
- wordpress - 仅当启用延期交货的产品缺货时,才在产品页面上显示自定义文本块
- windows - Git 挂起在 Windows 10 上运行任何命令
- docker - 我想部署一个镜像 Docker(也许是 tomcat ?或其他)