gpt4 book ai didi

Android - 在我的应用程序首选项中实现 ListPreferences

转载 作者:行者123 更新时间:2023-11-29 01:40:14 24 4
gpt4 key购买 nike

当从我的首选项中的 ListPreferences 中进行选择时,如何更改 Fragment 中微调器数组的内容?

在我的应用程序中,我尝试添加包含 ListPreferences 的首选项。我编写了设置 Activity,它显示了我的数组列表,但是当我选择一个选项时,我的应用程序没有任何变化。我的应用程序从主 Activity 运行 Fragments。我尝试使用用户选择的设置来操作从我的初始 Fragment 中显示的数据。这是我的代码:

设置 Activity :

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override
public void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
PreferenceManager.setDefaultValues(SettingsActivity.this, R.xml.settings, false);
//ListPreference lp = (ListPreference)findPreference("prefSections");
//lp.setEntries(new String [] {"prefSections", "PrefSectionsValues"});
getActionBar().setDisplayHomeAsUpEnabled(true);
}

@Override
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()){
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
default:
return super.onOptionsItemSelected(item);
}
}

@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {

}

最初显示的 fragment :

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

container.removeAllViews();

//Use View "v" containing the layout and container - use v.findViewById in others
View v = inflater.inflate(R.layout.mylayout, container, false);

SharedPreferences settingPrefs = PreferenceManager.getDefaultSharedPreferences(getActivity().getBaseContext());
String settingSection = settingPrefs.getString("prefSectionsValues", "1");
if (settingSection == "1"){
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_spinner_item, displayName);
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
spLoadFrom.setAdapter(spinnerArrayAdapter);
} else if (settingSection == "2"){
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_spinner_item, displayName2);
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
spLoadFrom.setAdapter(spinnerArrayAdapter);
} else if (settingSection == "3"){
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_spinner_item, displayName3);
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
spLoadFrom.setAdapter(spinnerArrayAdapter);
}else if (settingSection == "4"){
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_spinner_item, displayName4);
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
spLoadFrom.setAdapter(spinnerArrayAdapter);
}else if (settingSection == "5"){
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_spinner_item, displayName5);
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
spLoadFrom.setAdapter(spinnerArrayAdapter);
}
//ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_spinner_item, displayName);
//spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
//spLoadFrom.setAdapter(spinnerArrayAdapter);

SpinnerListener spListener = new SpinnerListener();

spLoadFrom.setOnItemSelectedListener(spListener);
// Inflate the layout for this fragment
return v;
}

数组.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>

<string-array name="prefSections">
<item name="sec">Split by Section</item>
<item name="secchap">Split by Section with Chapters</item>
<item name="secchapwhole">Split by Section with Chapters and the entire doc</item>
<item name="chap">Split by Chapters</item>
<item name="chapwhole">Split by Chapters and the entire doc</item>
</string-array>
<string-array name="prefSectionsValues">
<item>1</item>
<item>2</item>
<item>3</item>
<item>4</item>
<item>5</item>
</string-array>

</resources>

设置.xml:

<?xml version="1.0" encoding="utf-8"?>

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

<PreferenceCategory android:title="@string/prefUpdateCat">
<CheckBoxPreference
android:key="prefUpdate"
android:summary="On/Off"
android:title="Check for updates" />
</PreferenceCategory>

<PreferenceCategory android:title="@string/prefSectionsCat">
<ListPreference
android:key="pref_Sections"
android:entries="@array/prefSections"
android:entryValues="@array/prefSectionsValues"
android:summary="How it should be displayed"
android:title="it's displayed..." />
</PreferenceCategory>

</PreferenceScreen>

最佳答案

尝试更改您的 SettingsActivity 类以实现 SharedPreferences.OnSharedPreferenceChangeListener,这将使您在注册用户首选项更改时做出适当的响应。

public class SettingsActivity extends PreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener {
...
...
}


@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
Log.d(TAG, "Preferences have been changed");
//indicate fragment needs to update it's content
}

此外,您的 fragment 似乎没有更新,因为您显示用户首选项的逻辑位于 oncreateview 函数中,该函数仅在创建该 fragment 时被调用。 Read more about that here .基本上,onResume 函数比 onCreate 更好。只是不要在 onCreate 和 onResume 中复制您的代码,否则它会在您的 fragment 第一次创建时运行两次。

Override
public void onResume()
{
super.onResume();
//if spinnerarrayadapter is empty then populate as normal
//set as adapter for spLoadForm
//else if user preferences have changed
//populate spinnerarrayadapter here;
//spinnerarrayadapter.notifyDataSetChanged();
}

您可以使用在 onSharedPreferenceChange() 函数中设置的静态全局变量来跟踪用户首选项是否已更改。但是对于测试(或者如果您不认为用内容填充 spinnerarrayadapter 是一个缓慢的过程,当在最终发布产品中多次运行时会影响最终用户体验),您可以将其设为 else 语句。

希望这对您有所帮助。

关于Android - 在我的应用程序首选项中实现 ListPreferences,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25168574/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com