1

Let's say I have 10 buttons whose ID's are 1,2,...,10. I have an XML file called preferences.xml that contains a checkbox:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:key="applicationPreference" android:title="@string/config">
   <CheckBoxPreference
        android:key="checkbox" />                  
</PreferenceScreen>

My buttons all call the same function which calls for the intent to start the PreferenceActivity.

What I want to do is to call the same model with every button but save the values of the checkbox for each button. Currently, every time I click on a button, it launches the activity, but, for example, the value of my button 1 will be found in the value of my button 5.

Should I use SharedPreferences or something else?

I know it's possible but as I am still unfamiliar with many concepts, I just don't find it.

zuokuok
  • 323
  • 1
  • 6
  • 16

2 Answers2

0

If I understood correctly, you have to tell the PreferenceActivity which button was press. You can achieve this by setting the button id as a parameter when starting the activity.

/*
 * This is the common method used by all the buttons after the click 
 * was done in order to start the PreferenceActivity and pass
 * the button id as a parameter
 * @param sender - Button which was pressed and will start the
 * PreferenceActivity
*/
private void startPreferenceActivity(Button sender)
{
    // create the intent
    Intent intent=new Intent(context, PreferenceActivity.class);

    // add button id as a parameter
    intent.putExtra("buttonID", sender.getId());

    // start activity
    startActivity(intent);

    // or you can start it to way for a result 
    // startActivityForResult(intent, 0);
}

Now, in the PreferenceActivity you have to get the parameter which you have sent:

/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // get the extras where the parametrs are stored
    Bundle bundle = getIntent().getExtras();

    if(bundle.getInt("buttonID") != null)
    {
            // get id as int with default value as 0
            int id= intent.getInt("buttonID", 0);
    }

    if(bundle.getString("buttonID") != null)
    {
            // get id as string with default value as ""
            string id= intent.getString("buttonID", "");
    }

    // other code here ...

}

Hope this will help you.

Adrian C.
  • 1,647
  • 1
  • 23
  • 27
0

This is your problem: The PreferenceActivity automatically saves settings for you in the shared preferences of your application. For each setting, you define a key in your preferences.xml. In your case this is

android:key="checkbox"

so the value "checkbox" in your SP gets set to true, when you open your preferences with button 5. When you open the preferences with button 1 again, the PreferencesActivity looks up the SPs, sees that "checkbox" is true and therefore sets the checkbox to checked.

To avoid this issue, like Adrian said, you have to pass information to your PreferenceActivity about what button get clicked. In your PreferenceActivity you have to get a reference to your checkbox preference and add your passed button id to the key.

Preference checkboxPreference = findPreference("checkbox");
checkboxPreference.setKey(checkboxPreference.getKey() + buttonId);

Now there are 10 uniquely named boolean values ("checkbox1","checkbox2",..) saved in your SP for each of your buttons.

have fun

Lukas Lechner
  • 7,881
  • 7
  • 40
  • 53
  • This works. The value is indeed unique to each view. But that doesn't solve everything. My preference screen is in an XML file and nothing is added dynamically. This means that when I open my PreferenceScreen, the view shown is the one with the values of my preferences where the keys are the ones I set in the XML file. The actual values (with the new ID) are there but not displayed. When I click on the checkbox, the value of the new ID is changed, but the view displayed remains the first one. – zuokuok Oct 02 '15 at 16:58