保存并检索选定的微调器位置 [英] Save and Retrieve Selected Spinner Position

查看:86
本文介绍了保存并检索选定的微调器位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您将如何保存和检索微调器选择,以便当您返回微调器时选择同一项目?也许有共同的喜好?

How would you save and retrieve a spinners selection, so when you come back the same item on the spinner is selected? Maybe with shared preferences?

推荐答案

将数据保存在sharedPreferences上(将此代码放在onItemSelected()方法上,并保存spinner的选定值的位置):

to Save data on the sharedPreferences( put this code on the onItemSelected() method and save the selected value's position of your spinner) :

int userChoice = spinner.getSelectedItemPosition();
SharedPreferences sharedPref = getSharedPreferences("FileName",0);
SharedPreferences.Editor prefEditor = sharedPref.edit();
prefEditor.putInt("userChoiceSpinner",usersChoice);
prefEditor.commit();

要从sharedPreferences获取数据:

SharedPreferences sharedPref = getSharedPreferences("FileName",MODE_PRIVATE);
int spinnerValue = sharedPref.getInt("userChoiceSpinner",-1);
if(spinnerValue != -1) {
  // set the selected value of the spinner 
  spinner.setSelection(spinnerValue);
}

请参阅:微调器中的设置选择 然后:获取所选项目在微调器

Android教程:他们之间的活动和传递数据

这篇关于保存并检索选定的微调器位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆