Update Preference List - android

i'm trying to update my list in a preference with onPreferenceClick method. But the list isn't updated. onPreferenceClick is executed, because i got the names and showed it in the log, but not updated.
public class TransferPreference extends PreferenceFragment {
private TransferHelper transferHelper = new TransferHelper();
private ArrayList<String> names = new ArrayList<String>();
private CharSequence[] charSequence;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preference_transfer);
final MultiSelectListPreference preferenceTeams = (MultiSelectListPreference) findPreference("preferenceTranser_multiSelectListPreference_teams");
preferenceTeams.setPersistent(false);
preferenceTeams.setOnPreferenceClickListener(new OnPreferenceClickListener() {
#Override
public boolean onPreferenceClick(Preference preference) {
names = transferHelper.getServerTeams(getActivity());
int size = names.size();
charSequence = names.toArray(new CharSequence[size]);
preferenceTeams.setEntries(charSequence);
preferenceTeams.setEntryValues(charSequence);
return false;
}
});
}
}
I also tried this in onPreferenceClick:
Editor editor = preference.getEditor();
editor.clear();
names = transferHelper.getServerTeams(getActivity());
for (String name : names) {
editor.putString(name, name);
}
editor.commit();
If i put the following code direct in the oncreate method (and not in the listener method), the names are displayed. But only on its creation, like the name says :)
names = transferHelper.getServerTeams(getActivity());
int size = names.size();
charSequence = names.toArray(new CharSequence[size]);
preferenceTeams.setEntries(charSequence);
preferenceTeams.setEntryValues(charSequence);
Has anyone an idea?
Sorry for my bad english and thanks for help.
edit: i alos tried to add my code additional in this method, but no update.
#Override
public void onResume()

Related

Loading SharedPreferences in another activity

Here's the problem. In my second class, I'm trying to load the SharedPreferences. Below I'll also include my first class.
//set label for journal questions
public TextView journalQuestionLabel;
public int counter = 0;
SharedPreferences preferences;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_journal);
//TODO: Send saved preferences here
preferences = getSharedPreferences("grammarOption", MODE_PRIVATE);
int selection = preferences.getInt("grammarOption", -1);
Log.d("in onCreate", "preferences = " + selection);
}
When I test it, my debug log always prints -1. It won't load my shared preferences. What am I doing wrong?
I've tried the other answers on here and every tutorial, but they aren't working. Here is my code to set up and save my spinner preferences. I've checked this and it's working.
private void setupSpinner() {
// Create adapter for spinner. The list options are from the String array it will use
// the spinner will use the default layout
final ArrayAdapter grammarSpinnerAdapter = ArrayAdapter.createFromResource(this, R.array.array_grammar_options,
android.R.layout.simple_spinner_dropdown_item);
// Specify dropdown layout style - simple list view with 1 item per line
grammarSpinnerAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
//Apply the adapter to the spinner
grammarChoiceSpinner.setAdapter(grammarSpinnerAdapter);
//Create shared preferences to store the spinner selection
SharedPreferences preferences = getApplicationContext().getSharedPreferences
("Selection", MODE_PRIVATE);
editor = preferences.edit();
// Create the intent to save the position
grammarChoiceSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//receive the string of the option and store it
int grammarOptionPosition = grammarChoiceSpinner.getSelectedItemPosition();
//put the string in the editor
editor.putInt("grammarOption", grammarOptionPosition);
editor.commit();
//make a toast so the user knows if it's not "select"
if (grammarOptionPosition != 0) {
Toast.makeText(getApplicationContext(), "Choice saved.",
Toast.LENGTH_SHORT).show();
}
}
// Because AdapterView is an abstract class, onNothingSelected must be defined
#Override
public void onNothingSelected(AdapterView<?> parent) {
mGrammar = 0;
}
});
}
Here it's called in onCreate()
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_opening);
//find the spinner to read user input
grammarChoiceSpinner = (Spinner) findViewById(R.id.spinner);
setupSpinner();
You are passing wrong String in getSharedPreferences(String, int).
preferences = getSharedPreferences("Selection", MODE_PRIVATE);
int selection = preferences.getInt("grammarOption", -1);
Log.d("in onCreate", "preferences = " + selection);
Give this a try.
I hope it helps.
Make sure your SharedPreference key is same in both the class or even throughout the whole App.
While saving do like -
SharedPreferences preferences = getSharedPreferences("MY_PREFS", MODE_PRIVATE);
preferences.edit().putInt("grammerOption", 1).apply();
While Getting data from prefs do like -
SharedPreferences preferences = getSharedPreferences("MY_PREFS", MODE_PRIVATE);
int option = preference.getInt("grammerOption", -1);
PS,
Key for preference(which is MY_PREFS here) must be the same.

EditTextPreference value only refreshed after clicking ListPreference second time

I've been struggling with this issue for a while now so I decided to ask here what I' m doing wrong.
First of all:
- I have a PreferenceFragment with a ListPreference on top and an EditTextPreference below
- The ListPreference is filled with Objects, the values are stored in a file and read from there (this works flawlessly)
- The EditTextPreference should display the value of the in the ListPreference chosen object. And that's the problem: after choosing the value nothing changes so I have to click the ListPreference once more and the value is set correctly. Is this a problem with my Listener?
Here's the code:
public class SettingsTestFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener {
private final String[] pref_key_array = {"pref_key_lp", "pref_key_et""}; // array that contains all the preference keys of changeable values
private final int numberOfEntries = pref_key_array.length;
private Preference[] pref_entries;
String[] entries = {"Value 1", "Value 2", "Value 3"};
String[] entryValues = {"0", "1", "2"};
private int position;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
final SharedPreferences myPreference = PreferenceManager.getDefaultSharedPreferences(getActivity());
final EditTextPreference et = (EditTextPreference) findPreference("pref_key_et");
final ListPreference lp = (ListPreference) findPreference("pref_key_lp");
prepareListPref(lp);
pref_entries = new Preference[numberOfEntries];
for(int i = 0; i < numberOfEntries; i++) {
pref_entries[i] = getPreferenceScreen().findPreference(pref_key_array[i]);
}
lp.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
#Override
public boolean onPreferenceClick(Preference preference) {
position = Integer.valueOf(myPreference.getString("pref_key_lp", "0"));
et.setText(entries[position]);
return true;
}
});
Preference.OnPreferenceChangeListener changeListener = new Preference.OnPreferenceChangeListener() {
public boolean onPreferenceChange(Preference preference, Object newValue) {
position = Integer.valueOf(myPreference.getString("pref_key_lp", "0"));
preference.setSummary(entries[position]);
return true;
}
};
lp.setOnPreferenceChangeListener(changeListener);
}
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
updateSummary(key, pref_key_array, numberOfEntries, pref_entries);
}
#Override
public void onResume() {
super.onResume();
// Set up listener when a key changes
for(int i = 0; i < numberOfEntries; i++) {
updateSummary(pref_key_array[i], pref_key_array, numberOfEntries, pref_entries);
}
getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
}
#Override
public void onPause() {
super.onPause();
// Unregister listener every time a key changes
getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
}
public void prepareListPref(ListPreference lp) {
lp.setEntries(entries);
lp.setEntryValues(entryValues);
lp.setDefaultValue("0");
}
public void updateSummary(String key, String[] pref_key_array, int numberOfEntries, Preference[] pref_entries) {
for(int i = 0; i < numberOfEntries; i++) {
if(key.equals(pref_key_array[i])) {
if(pref_entries[i] instanceof EditTextPreference) {
final EditTextPreference currentPreference = (EditTextPreference) pref_entries[i];
pref_entries[i].setSummary(currentPreference.getText());
} else if(pref_entries[i] instanceof ListPreference) {
final ListPreference currentPreference = (ListPreference) pref_entries[i];
pref_entries[i].setSummary(currentPreference.getEntry());
}
break;
}
}
}
}
Summarizing the code for reading from the file and writing the value to the Settings works but only after clicking the ListPreference a second time. Do you have any ideas why?
Thanks
ok, I'm not sure what you are trying to do and what is the problem, so I've made a sample, showing the next thing:
a listPreference that its default value&entry will set the title&summary of an EditTextPreference .
when choosing an item on the ListPreference, it will also update tge title&summary of the EditTextPreference according to the value&entry of the item being selected.
Not sure what to do with the EditTextPreference. This is your choice.
I still think you should consider making a custom Preference class, as you wrote that you intend to use a lot of couples of ListPreference&EditTextPreference.
BTW, code is based on an app that I've made (link here). I've made it so that it will be easy to handle multiple listPreferences easier.
Here's the code:
res/values/strings_activity_settings.xml
<resources>
<string name="pref__custom_app_theme" translatable="false">pref__custom_app_theme</string>
<string name="pref__app_theme" translatable="false">pref__app_theme</string>
<string-array name="pref__app_theme_entries">
<item>#string/cards_light</item>
<item>#string/cards_dark</item>
</string-array>
<string name="pref__app_theme__cards_ui" translatable="false">CARDS_UI</string>
<string name="pref__app_theme__cards_ui_dark" translatable="false">CARDS_UI_DARK</string>
<string name="pref__app_theme_default" translatable="false">#string/pref__app_theme__cards_ui</string>
<string-array name="pref__app_theme_values">
<item>#string/pref__app_theme__cards_ui</item>
<item>#string/pref__app_theme__cards_ui_dark</item>
</string-array>
</resources>
res/values/strings.xml
<resources>
<string name="app_name">My Application</string>
<string name="app_theme">App Theme</string>
<string name="cards_light">cards light</string>
<string name="cards_dark">cards dark</string>
</resources>
res/xml/pref_general.xml
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<!-- theme -->
<ListPreference
android:defaultValue="#string/pref__app_theme_default"
android:entries="#array/pref__app_theme_entries"
android:entryValues="#array/pref__app_theme_values"
android:key="#string/pref__app_theme"
android:title="#string/app_theme"/>
<EditTextPreference android:key="#string/pref__custom_app_theme"/>
</PreferenceScreen>
SettingsActivity.java
public class SettingsActivity extends PreferenceActivity
{
public interface IOnListPreferenceChosenListener
{
public void onChosenPreference(String key,String entry,String value);
}
#SuppressWarnings("deprecation")
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.pref_general);
final EditTextPreference editTextPreference=(EditTextPreference)findPreference(getString(R.string.pref__custom_app_theme));
final ListPreference listPreference=prepareListPreference(this,R.string.pref__app_theme,R.array.pref__app_theme_entries,R.array.pref__app_theme_values,R.string.pref__app_theme_default,new IOnListPreferenceChosenListener()
{
#Override
public void onChosenPreference(final String key,final String entry,final String value)
{
editTextPreference.setTitle(value);
editTextPreference.setSummary(entry);
}
});
editTextPreference.setTitle(listPreference.getValue());
editTextPreference.setSummary(listPreference.getEntry());
}
public static ListPreference prepareListPreference(final PreferenceActivity activity,final int prefKeyId,//
final int entriesId,final int valuesId,final int defaultValueId,final IOnListPreferenceChosenListener listener)
{
final String prefKey=activity.getString(prefKeyId);
#SuppressWarnings("deprecation")
final ListPreference pref=(ListPreference)activity.findPreference(prefKey);
final String[] entries=activity.getResources().getStringArray(entriesId);
final String[] values=activity.getResources().getStringArray(valuesId);
final String defaultValue=activity.getResources().getString(defaultValueId);
final String currentValue=PreferenceManager.getDefaultSharedPreferences(activity).getString(prefKey,defaultValue);
for(int i=0;i<values.length;++i)
{
final String value=values[i];
if(TextUtils.equals(currentValue,value))
{
pref.setSummary(entries[i]);
pref.setValueIndex(i);
break;
}
}
pref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener()
{
#Override
public boolean onPreferenceChange(final Preference preference,final Object newValue)
{
final String newValueStr=newValue.toString();
String entryChosen=null;
for(int i=0;i<values.length;++i)
{
final String value=values[i];
if(TextUtils.equals(newValueStr,value))
{
entryChosen=entries[i];
break;
}
}
pref.setSummary(entryChosen);
if(listener!=null)
listener.onChosenPreference(prefKey,entryChosen,newValueStr);
return true;
}
});
return pref;
}
}

How to put score from one activity into another activity without going to that activity?

I am making a game like logo quiz. I have the question activity and the levels activity so when users answer correctly they score 1. Then I want to put the score in the levels activity so in that way users could unlock the next level, but I don't want users leave the question activity and until now I have only found this method:
Intent resultIntent = new Intent(this, NextActivity.class);
resultIntent.putExtra("score", score);
startActivity(resultIntent);
However, with this method the user goes to the levels activity.
I will leave my code for reference:
public class Big extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_big);
init();
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true); }
public boolean onOptionsItemSelected(MenuItem item){
Intent myIntent = new Intent(getApplicationContext(), Level1.class);
startActivityForResult(myIntent, 0);
return true;
}
private Button buttonSaveMem1;
private EditText escrive;
private TextView respuest;
private String [] answers;
int score=0;
int HighScore;
private String saveScore = "HighScore";
private int currentQuestion;
public void init()
{
answers = new String[]{"Big"};
buttonSaveMem1 = (Button)findViewById(R.id.button1);
respuest = (TextView) findViewById(R.id.textView2);
escrive = (EditText) findViewById(R.id.editText1);
buttonSaveMem1.setOnClickListener(buttonSaveMem1OnClickListener);
LoadPreferences();
}
Button.OnClickListener buttonSaveMem1OnClickListener
= new Button.OnClickListener(){
#Override
public void onClick(View arg0) {
checkAnswer();
// TODO Auto-generated method stub
SavePreferences();
LoadPreferences();
}};
public boolean isCorrect(String answer)
{
return (answer.equalsIgnoreCase(answers[currentQuestion]));
}
public void checkAnswer() {
String answer = escrive.getText().toString();
if(isCorrect(answer)) {
update();
respuest.setText("You're right!" + " The Answer is " + answer + " your score is:" + score +" " +
"HighScore: " + HighScore);
score =1;
}
else {
respuest.setText("Sorry, The answer is not right!");
}
}
private void update() {
if (score > HighScore)
{ HighScore = score; }
}
private void SavePreferences(){
SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("MEM1", respuest.getText().toString());
sharedPreferences.edit().putInt(saveScore, HighScore).commit();
editor.commit();
}
private void LoadPreferences(){
SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
String strSavedMem1 = sharedPreferences.getString("MEM1", "");
HighScore = sharedPreferences.getInt(saveScore, 0);
respuest.setText(strSavedMem1);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
And here is the levels activity:
public class Level extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_level);
Button salir = (Button) findViewById(R.id.button3);
salir.setOnClickListener( new View.OnClickListener() {
#Override public void onClick(View v) {
startActivity(new Intent(Level.this, MainActivity.class)); }
}
)
;
Button leve2 = (Button) findViewById(R.id.button1);
leve2.setOnClickListener( new View.OnClickListener() {
#Override public void onClick(View v) {
startActivity(new Intent(Level.this, Level2.class)); }
}
)
; }
Button leve1 = (Button) findViewById(R.id.button1);
leve1.setOnClickListener( new View.OnClickListener() {
#Override public void onClick(View v) {
startActivity(new Intent(Level.this, Level1.class)); }
}
)
;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.level, menu);
return true;
}
}
Thanks for the help!
In your questions activity, store the score of the user in the SharedPreferences
SharedPreferences prefs = getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
prefs.edit.putLong(USER_SCORE, score).commit();
And then when you return to your level's activity, you can fetch from the preferences.
SharedPreferences prefs = getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
long userScore = prefs.getLong(USER_SCORE, 0);
USER_SCORE is just a string key like USER_SCORE = "user_score" to allow the device to find the date you stored in the prefs.
Shared preferences are saved to the phone and not accessible except through the app that they belong to. So upon starting the app again, you can get the User's score that was saved last time they used the app.
You can make make the score as static and then modify it from the other activity class. IT would automatically change it in the original.
Store the score in a SharedPreferences instead of passing it to Level in an intent. You can then retrieve that score within the levels Activity (or any other for that matter), whenever the user may navigate there. You already use SharedPreferences in your code with:
SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
However that returns a Shared Preference using the calling Activity's class name as the Shared Preference name, i.e. those preference values are private to your Activity 'Big'. To use preference values that have application scope, use getSharedPreferences(), providing a Shared Preferences name:
SharedPreferences sharedPreferences = getSharedPreferences("MYPREFS", Activity.MODE_PRIVATE);
Create an Editor from that and store the value of 'score'. Then retrieve it your Level activity, most likely in its onCreate().
After looking here and there, I've finally found out my answer to this question by following other answers and I basically used the following combination of codes to do so.
In a first activity:
import:
import android.content.Context;
import android.content.SharedPreferences;
declare:
public static int totalCount;
and add onCreate():
SharedPreferences prefs = getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
totalCount = prefs.getInt("counter", 0);`
totalCount++;`
editor.putInt("counter", totalCount);`
editor.apply();`
Then, on a second activity:
import:
import static com.example.myapp.totalCount
and add onCreate():
((TextView) findViewById(R.id.text_view_id)).setText(String.valueOf(totalCount));
In the layout for the second activity:
place a TextView with:
android:id="#+id/text_view_id"
And pay attention to what the documentation says about naming shared preferences.
When naming your shared preference files, you should use a name that's
uniquely identifiable to your app. An easy way to do this is prefix
the file name with your application ID. For example:
"com.example.myapp.PREFERENCE_FILE_KEY"

SharedPreferences Android - Saving and editing one string only using two activities

I have a single string which the user will edit and will be displayed back to him when he uses the app. He can edit the string at any time. I am familiar with SQLite databases, but because for this purpose I am using only one string/one record, I felt SharedPreferences would be better. However, after following two different tutorials, I am unable to get it so save the data. In both cases I have needed to amend the tutorial code because I will be using two activities, one to view the code, the other to edit it. I was unable to find a tutorial for using sharedpreferences for two activities. Below is the code.
Class to view the code:
public class MissionOverviewActivity extends Activity {
TextView textSavedMem1;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mission_view);
textSavedMem1 = (TextView)findViewById(R.id.textSavedMem1);
LoadPreferences();
textSavedMem1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
finish();
return;
}});
};
private void LoadPreferences(){
SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
String strSavedMem1 = sharedPreferences.getString("MEM1", "");
textSavedMem1.setText(strSavedMem1);
}
}
Class to edit the code and return to the view page
public class MissionDetailActivity extends Activity {
EditText editText1;
Button buttonSaveMem1;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mission_edit);
editText1 = (EditText)findViewById(R.id.editText1);
buttonSaveMem1 = (Button)findViewById(R.id.buttonSaveMem1);
buttonSaveMem1.setOnClickListener(buttonSaveMem1OnClickListener);
}
Button.OnClickListener buttonSaveMem1OnClickListener
= new Button.OnClickListener(){
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
SavePreferences("MEM1", editText1.getText().toString());
viewStatement();
}
};
private void SavePreferences(String key, String value){
SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(key, value);
editor.commit();
}
protected void viewStatement() {
Intent i = new Intent(this, MissionOverviewActivity.class);
startActivity(i);
}
}
If any body could answer this question, or point me in the direction of a sharedpreferences tutorial that uses two classes (for edit and displaying), It would be greatly appreciated!
Thanks
getPreferences(int) is private for Activity, you want to share the same SharedPreference between activities you should use this way:
SharedPreferences prefs = this.getSharedPreferences(
"yourfilename", Context.MODE_PRIVATE);
and use the same method when you want to reload it. here the doc for getPrerences(int)

Check/Uncheck all preferences?

I'm trying to improve the user friendliness of an app by providing the user with "Select all" and "Deselect all" options in the preferences. Things seem to be working, except for one major flaw:
I'm using SharedPreferences.getAll() to retrieve all the checkboxes so I can iterate them and check/uncheck them. But it seems that getAll() doesn't quite live up to it's name. It doesn't return ALL preferences, but only the ones that have been previously altered by the user.
So, is there a way to retrieve ALL of the preferences?
The code I'm currently using:
public class ADRpreferences extends PreferenceActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
((Preference) findPreference("searchresult_select_all")).
setOnPreferenceClickListener(new OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference preference) {
setCheckState("searchresult", true);
return true;
}
});
((Preference) findPreference("searchresult_deselect_all")).
setOnPreferenceClickListener(new OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference preference) {
setCheckState("searchresult", false);
return true;
}
});
}
private void setCheckState(String prefix, Boolean state) {
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
#SuppressWarnings("unchecked")
Map<String, Boolean> categories = (Map<String, Boolean>) settings.getAll();
for (String s : categories.keySet()) {
Preference pref = findPreference(s);
if( s.startsWith(prefix) && (pref instanceof CheckBoxPreference) ){
((CheckBoxPreference) pref).setChecked(state);
}
}
}
}
EDIT
Ok, for now I will go with MH's solution. I put all the ID's of the checkbox preferences into arrays.xml, and will use those values to iterate through all the checkboxes (there are 44 of them in case you wonder why I just don't hardcode the ID's). Works great, of course in the future I will have to remember to add/remove the ID's in the arrays if I make changes to the preferences. Here's the new setCheckState():
private void setCheckState(String category, Boolean state) {
String[] arr = null;
if( category.equals("loadlist") ){
arr = getResources().getStringArray(R.array.array_loadlist_checkboxes);
}
else if( category.equals("searchresult") ){
arr = getResources().getStringArray(R.array.array_searchresult_checkboxes);
}
else{
return;
}
for( String s : arr ){
Preference pref = findPreference(s);
if( pref instanceof CheckBoxPreference ){
((CheckBoxPreference) pref).setChecked(state);
}
}
}

Categories

Resources