如何使用共享首选项将所选值保存在微调器中



我的警报对话框中有微调器。现在,我想使用共享首选项保存微调器所选项目的数据。我检查了很多问题,但不知道如何使用我的代码。请帮帮我。

这是我的微调器代码,我在其中检查用户选择了哪个值?

case R.id.location:
final AlertDialog.Builder mbuilder = new AlertDialog.Builder(HomeActivity.this);
View v = getLayoutInflater().inflate(R.layout.dialog_layout, null);
Button okbutton = v.findViewById(R.id.okbutton);
final Spinner spinner = (Spinner) v.findViewById(R.id.spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(HomeActivity.this, android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.countries_list));
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
mbuilder.setView(v);
final AlertDialog dialog = mbuilder.create();
okbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.e("Dialog123", "onClick: Positive" );
String text = spinner.getSelectedItem().toString();
newsModels = new ArrayList<>();
if(text.equals("INDIA")) {
newsModels.clear();
TypedArray indianewslogo = getResources().obtainTypedArray(R.array.indianews);
String[] indianewsname = getResources().getStringArray(R.array.indianewsnames);
String[] indianewsurl = getResources().getStringArray(R.array.indiahref);
for(int i =0; i<indianewslogo.length(); i++) {
//SendModel sendModel = new SendModel(indianewslogo.getResourceId(i,0),indianewsname[i]);
newsModels.add(new NewsModel(indianewslogo.getResourceId(i,0),indianewsname[i], indianewsurl[i]));
}
Bundle bundle = new Bundle();
bundle.putParcelableArrayList("mylist", newsModels);
HomeFragment homeFragment = new HomeFragment();
homeFragment.setArguments(bundle);
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.frame_layout, homeFragment);
fragmentTransaction.commit();
binding.bottmNav.getMenu().findItem(R.id.location).setTitle("India");
binding.bottmNav.getMenu().findItem(R.id.home1).setChecked(true);
dialog.dismiss();
}
}
});

这就是您应该如何使用SharedPreferences:

SharedPreferences sharedPreferences = getSharedPreferences("MySharedPreference", MODE_PRIVATE); 
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("key", "value that you want to save");
editor.commit(); 

参考:https://developer.android.com/training/data-storage/shared-preferences

这就是如何实现您的代码:


SharedPreferences sharedPreferences = getSharedPreferences("MySharedPreference", MODE_PRIVATE); 
SharedPreferences.Editor editor = sharedPreferences.edit();
... // your code
switch() {
case R.id.location:
final AlertDialog dialog = mbuilder.create();
... // your code
okbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.e("Dialog123", "onClick: Positive" );
String text = spinner.getSelectedItem().toString();
editor.putString("selected country", text);
editor.commit(); 
... // your code
}
});

如果你想获取数据,你可以这样称呼它:

sharedPreferences.getString("selected country", ""); // empty means default value should be string

您可以使用此代码保存本地值:

SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(keyString, valueString);
editor.commit();

在你的代码中,你可以这样工作:

case R.id.location:
final AlertDialog.Builder mbuilder = new AlertDialog.Builder(HomeActivity.this);
View v = getLayoutInflater().inflate(R.layout.dialog_layout, null);
Button okbutton = v.findViewById(R.id.okbutton);
final Spinner spinner = (Spinner) v.findViewById(R.id.spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(HomeActivity.this, android.R.layout.simple_spinner_item,
getResources().getStringArray(R.array.countries_list));
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
mbuilder.setView(v);
final AlertDialog dialog = mbuilder.create();

okbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.e("Dialog123", "onClick: Positive" );
String text = spinner.getSelectedItem().toString();
//here save  the selected value of your spinner by this
SharedPreferences.Editor editor =   sharedPreferences.edit();
editor.putString("my_selected_value", text);
editor.commit();
newsModels = new ArrayList<>();
if(text.equals("INDIA")) {
newsModels.clear();
TypedArray indianewslogo = getResources().obtainTypedArray(R.array.indianews);
String[] indianewsname = getResources().getStringArray(R.array.indianewsnames);
String[] indianewsurl = getResources().getStringArray(R.array.indiahref);
for(int i =0; i<indianewslogo.length(); i++) {
//SendModel sendModel = new SendModel(indianewslogo.getResourceId(i,0),indianewsname[i]);
newsModels.add(new NewsModel(indianewslogo.getResourceId(i,0),indianewsname[i], indianewsurl[i]));
}
Bundle bundle = new Bundle();
bundle.putParcelableArrayList("mylist", newsModels);
HomeFragment homeFragment = new HomeFragment();
homeFragment.setArguments(bundle);
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.frame_layout, homeFragment);
fragmentTransaction.commit();
binding.bottmNav.getMenu().findItem(R.id.location).setTitle("India");
binding.bottmNav.getMenu().findItem(R.id.home1).setChecked(true);
dialog.dismiss();

相关内容

  • 没有找到相关文章

最新更新