user3567001
user3567001

Reputation: 61

Android - Implementing ListPreferences in my apps Preferences

How would I change the content of a spinner array in a Fragment when a selection is made from ListPreferences in my Preferences?

In my app I am trying to add preferences that include ListPreferences. I have the settings Activity written and it shows my array list but when I select an option nothing changes in my app. My app runs Fragments from a main Activity. I try to manipulate data that is shown from my initial Fragment with the settings that are chosen by the user. Here is my code:

SettingsActivity:

@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) {

}

Initially shown 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;
}

arrays.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>

settings.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>

Upvotes: 2

Views: 1208

Answers (1)

skyjacks
skyjacks

Reputation: 1154

Try changing your SettingsActivity class to implement SharedPreferences.OnSharedPreferenceChangeListener which would let you respond appropriately when a change in user preferences is registered.

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
}

Also it looks like your fragment isn't getting updated because your logic to display user preferences is in the oncreateview function which only gets called when that fragment is being created. Read more about that here. Basically, the onResume function would be a better place instead of onCreate. Just don't replicate your code in both onCreate and onResume otherwise it'll run twice the first time your fragment is created.

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();
}

You could keep track of whether user preferences have changed by using a static global variable that you set in the onSharedPreferenceChange() function. For testing though (or if you don't think populating the spinnerarrayadapter with content is a slow process that would impact end user experience when run multiple times in a final release product) you could just make it an else statement.

Hope this helps.

Upvotes: 1

Related Questions