腾讯云
开发者社区
文档
建议反馈
控制台
登录/注册
首页
学习
活动
专区
圈层
工具
MCP广场
文章/答案/技术大牛
搜索
搜索
关闭
发布
首页
标签
sharedpreferences
#
sharedpreferences
关注
专栏文章
(18)
技术视频
(0)
互动问答
(3)
ExpandableListView 控件+Spinner 如何用SharedPreferences 保存上一次选择的值???
1
回答
expandablelistview
、
sharedpreferences
、
spinner
gavin1024
在Android中,使用SharedPreferences可以保存和读取用户的设置信息。要在ExpandableListView控件和Spinner中保存上一次选择的值,可以按照以下步骤操作: 1. 首先,创建一个SharedPreferences对象,用于存储和读取数据。 ```java SharedPreferences sharedPreferences = getSharedPreferences("app_settings", MODE_PRIVATE); ``` 2. 在ExpandableListView的子项点击事件中,保存选择的值。 ```java expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() { @Override public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { // 保存选择的值 SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putInt("last_expandablelistview_selection", childPosition); editor.apply(); return false; } }); ``` 3. 在Spinner的选择事件中,保存选择的值。 ```java spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { // 保存选择的值 SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putInt("last_spinner_selection", position); editor.apply(); } @Override public void onNothingSelected(AdapterView<?> parent) { } }); ``` 4. 在Activity的onCreate方法中,读取上一次保存的值,并设置为当前选择。 ```java @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 读取上一次保存的值 int lastExpandableListViewSelection = sharedPreferences.getInt("last_expandablelistview_selection", -1); int lastSpinnerSelection = sharedPreferences.getInt("last_spinner_selection", -1); // 设置为当前选择 if (lastExpandableListViewSelection != -1) { expandableListView.setSelection(lastExpandableListViewSelection); } if (lastSpinnerSelection != -1) { spinner.setSelection(lastSpinnerSelection); } } ``` 通过以上步骤,可以实现在ExpandableListView控件和Spinner中保存上一次选择的值。在腾讯云产品中,您可以考虑使用腾讯云对象存储(COS)或腾讯云数据库(TencentDB)来存储和管理用户的设置信息。...
展开详请
赞
0
收藏
0
评论
0
分享
在Android中,使用SharedPreferences可以保存和读取用户的设置信息。要在ExpandableListView控件和Spinner中保存上一次选择的值,可以按照以下步骤操作: 1. 首先,创建一个SharedPreferences对象,用于存储和读取数据。 ```java SharedPreferences sharedPreferences = getSharedPreferences("app_settings", MODE_PRIVATE); ``` 2. 在ExpandableListView的子项点击事件中,保存选择的值。 ```java expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() { @Override public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { // 保存选择的值 SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putInt("last_expandablelistview_selection", childPosition); editor.apply(); return false; } }); ``` 3. 在Spinner的选择事件中,保存选择的值。 ```java spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { // 保存选择的值 SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putInt("last_spinner_selection", position); editor.apply(); } @Override public void onNothingSelected(AdapterView<?> parent) { } }); ``` 4. 在Activity的onCreate方法中,读取上一次保存的值,并设置为当前选择。 ```java @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 读取上一次保存的值 int lastExpandableListViewSelection = sharedPreferences.getInt("last_expandablelistview_selection", -1); int lastSpinnerSelection = sharedPreferences.getInt("last_spinner_selection", -1); // 设置为当前选择 if (lastExpandableListViewSelection != -1) { expandableListView.setSelection(lastExpandableListViewSelection); } if (lastSpinnerSelection != -1) { spinner.setSelection(lastSpinnerSelection); } } ``` 通过以上步骤,可以实现在ExpandableListView控件和Spinner中保存上一次选择的值。在腾讯云产品中,您可以考虑使用腾讯云对象存储(COS)或腾讯云数据库(TencentDB)来存储和管理用户的设置信息。
ExpandableListView 控件+Spinner 如何用SharedPreferences 保存上一次选择的值?
1
回答
expandablelistview
、
sharedpreferences
、
spinner
gavin1024
在Android中,使用SharedPreferences可以保存和读取用户的设置信息。要在ExpandableListView控件和Spinner中保存上一次选择的值,你可以按照以下步骤操作: 1. 首先,创建一个SharedPreferences对象,用于存储和读取数据。 ```java SharedPreferences sharedPreferences = getSharedPreferences("app_settings", MODE_PRIVATE); ``` 2. 在ExpandableListView的子项点击事件中,保存选择的值。 ```java expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() { @Override public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { // 保存选择的值 SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putInt("last_expandablelistview_selection", childPosition); editor.apply(); return false; } }); ``` 3. 在Spinner的选择事件中,保存选择的值。 ```java spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { // 保存选择的值 SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putInt("last_spinner_selection", position); editor.apply(); } @Override public void onNothingSelected(AdapterView<?> parent) { } }); ``` 4. 在Activity的onCreate方法中,读取上一次保存的值,并设置为当前选择。 ```java @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 读取上一次保存的值 int lastExpandableListViewSelection = sharedPreferences.getInt("last_expandablelistview_selection", -1); int lastSpinnerSelection = sharedPreferences.getInt("last_spinner_selection", -1); // 设置为当前选择 if (lastExpandableListViewSelection != -1) { expandableListView.setSelection(lastExpandableListViewSelection); } if (lastSpinnerSelection != -1) { spinner.setSelection(lastSpinnerSelection); } } ``` 通过以上步骤,你可以在ExpandableListView控件和Spinner中保存上一次选择的值。在腾讯云产品中,你可以考虑使用腾讯云对象存储(COS)来存储用户的设置信息。腾讯云对象存储提供了高可靠性、高扩展性的存储服务,可以满足各种应用场景的需求。...
展开详请
赞
0
收藏
0
评论
0
分享
在Android中,使用SharedPreferences可以保存和读取用户的设置信息。要在ExpandableListView控件和Spinner中保存上一次选择的值,你可以按照以下步骤操作: 1. 首先,创建一个SharedPreferences对象,用于存储和读取数据。 ```java SharedPreferences sharedPreferences = getSharedPreferences("app_settings", MODE_PRIVATE); ``` 2. 在ExpandableListView的子项点击事件中,保存选择的值。 ```java expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() { @Override public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { // 保存选择的值 SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putInt("last_expandablelistview_selection", childPosition); editor.apply(); return false; } }); ``` 3. 在Spinner的选择事件中,保存选择的值。 ```java spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { // 保存选择的值 SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putInt("last_spinner_selection", position); editor.apply(); } @Override public void onNothingSelected(AdapterView<?> parent) { } }); ``` 4. 在Activity的onCreate方法中,读取上一次保存的值,并设置为当前选择。 ```java @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 读取上一次保存的值 int lastExpandableListViewSelection = sharedPreferences.getInt("last_expandablelistview_selection", -1); int lastSpinnerSelection = sharedPreferences.getInt("last_spinner_selection", -1); // 设置为当前选择 if (lastExpandableListViewSelection != -1) { expandableListView.setSelection(lastExpandableListViewSelection); } if (lastSpinnerSelection != -1) { spinner.setSelection(lastSpinnerSelection); } } ``` 通过以上步骤,你可以在ExpandableListView控件和Spinner中保存上一次选择的值。在腾讯云产品中,你可以考虑使用腾讯云对象存储(COS)来存储用户的设置信息。腾讯云对象存储提供了高可靠性、高扩展性的存储服务,可以满足各种应用场景的需求。
删除共享首选项?
2
回答
sharedpreferences
、
测试
、
服务
、
数据
、
同步
MyLove
我的解决方案 SharedPreferences preferences = getSharedPreferences("Mypref", 0); preferences.edit().remove("shared_pref_key").commit();...
展开详请
赞
0
收藏
0
评论
0
分享
我的解决方案 SharedPreferences preferences = getSharedPreferences("Mypref", 0); preferences.edit().remove("shared_pref_key").commit();
热门
专栏
swag code
51 文章
13 订阅
Android 开发者
626 文章
66 订阅
Nicky's blog
859 文章
31 订阅
程序员修炼之路
261 文章
33 订阅
领券