首页
学习
活动
专区
圈层
工具
发布
首页标签sharedpreferences

#sharedpreferences

ExpandableListView 控件+Spinner 如何用SharedPreferences 保存上一次选择的值???

在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)来存储和管理用户的设置信息。... 展开详请
在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 保存上一次选择的值?

在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)来存储用户的设置信息。腾讯云对象存储提供了高可靠性、高扩展性的存储服务,可以满足各种应用场景的需求。... 展开详请
在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)来存储用户的设置信息。腾讯云对象存储提供了高可靠性、高扩展性的存储服务,可以满足各种应用场景的需求。

删除共享首选项?

我的解决方案 SharedPreferences preferences = getSharedPreferences("Mypref", 0); preferences.edit().remove("shared_pref_key").commit();... 展开详请
领券