0

I'm building an app where I need to show a settings screen when user can check what items from which categories will be displayed in main activity's listview.

I'm parsing an XML for these categories, so the PreferenceScreen is done programatically and all the CheckBoxPreferences are build in a loop. Code:

try {
    URL url = new URL("http://www.someurl.com/phone/categories.php");

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.parse(new InputSource(url.openStream()));
    doc.getDocumentElement().normalize();

    NodeList nodeList = doc.getElementsByTagName("category");

    for (int i = 0; i < nodeList.getLength(); i++) {                
        Element category = (Element)nodeList.item(i);

        Node id = category.getElementsByTagName("id").item(0);
        Node title = category.getElementsByTagName("name").item(0);

        CheckBoxPreference togglePref = new CheckBoxPreference(this);
        togglePref.setKey("category_" + id.getChildNodes().item(0).getNodeValue());
        togglePref.setDefaultValue(true);
        togglePref.setTitle(title.getChildNodes().item(0).getNodeValue());
        root.addPreference(togglePref);
    }
} catch(Exception ex) {
    Log.e(this.getClass().getSimpleName(), ex.getMessage());
}

where root is PreferenceScreen object.

My problem is that I need to store not only a title for one checkbox option but I also need to store an ID of the category but there is no such property in CheckBoxPreference object.

My question is if there is another workaround or should I extend the CheckBoxPreference class and create some custom MyCheckBoxPreference that will store also another "value"?

My vision is to have something similar (or same) as common HTML checkbox - while it has a value this is not set (submitted) if checkbox is not checked and on other hand when checked You obtain the concrete value instead of just true/false...

AFAIK the CheckBoxPreference has only state checked/unchecked while no value that could be returned.

I was thinking about having the preference keys set just to the ID of concrete cetagories instead of category_<ID> and then calling sharedPreferences.getAll() - I expect that only checked CheckBoxPreferences should be returned and then by obtaining of their keys I should be able to operate with the category IDs... Could this be an easy solution and good approach???

Many thanks for any proper advice!!!

shadyyx
  • 15,825
  • 6
  • 60
  • 95

1 Answers1

1

My problem is that I need to store not only a title for one checkbox option but I also need to store an ID of the category but there is no such property in CheckBoxPreference object.

IMHO, that is what the key is for.

I was thinking about having the preference keys set just to the ID of concrete cetagories instead of category_ and then calling sharedPreferences.getAll() - I expect that only checked CheckBoxPreferences should be returned and then by obtaining of their keys I should be able to operate with the category IDs... Could this be an easy solution and good approach???

You will get all CheckBoxPreference objects that have been touched by the user, whether they are checked or unchecked. This is probably the correct approach.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • OK, I thought that the key is like a preference unique identifier, not it's value... But I'll give it a try... – shadyyx Apr 19 '11 at 23:23
  • @shadyyx: It is the preference unique identifier. However, for a boolean state, you need no other "value" than the identifier and whether or not it is checked. – CommonsWare Apr 19 '11 at 23:31
  • @CommonsWare is it possible to change the state of checkbox based on a condition and not if the user clicks on it or not ? – Rain Man May 28 '16 at 05:05
  • @RainMan: Um, do you [mean `setChecked()`](https://developer.android.com/reference/android/preference/TwoStatePreference.html#setChecked%28boolean%29)? – CommonsWare May 28 '16 at 10:54
  • @CommonsWare the thing is that I want to use checkboxpreference for in app purchase, so if the purchase fails uncheck the box and if success check the box. But, no matter what I do, the box is checked even if the purchase fails. I asked a question [here](http://stackoverflow.com/questions/37475618/implement-in-app-billing-using-the-checkboxpreference). Any help will be appreciated. – Rain Man May 28 '16 at 17:57