开发者

Android - How to set a value for CheckBoxPreference? Or?

I'm building an app where I need to show a settings screen when user c开发者_高级运维an 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!!!


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜