【问题标题】:adding a JSON array (from an API) to a listpreference on the android SDK将 JSON 数组(来自 API)添加到 android SDK 上的 listpreference
【发布时间】:2011-02-16 17:58:46
【问题描述】:

我需要将从 json api(也可以使用 xml)获得的列表放入 listpreference
谁能尽可能好地解释我该怎么做?
来自 api 的示例输出:

JSON: (preferred) 
     [
     "+32486000001",
     "+32486000002" ]


xml: <response>
     <resource>+32486000001</resource>
     <resource>+32486000002</resource>
     </response>

我已经得到它的代码:

    String username = prefs.getString("username", null);
    String password = prefs.getString("password", null);
    String response = MVDataHelper.getResponse(username, password, URL_GET_MSISDN);

所以我需要将 JSonarray 转换为适合 listpreference 的格式以及将此函数添加到数组的方法。

请尽快帮助我

提前致谢

ps:条目和条目值都应该是我在这里得到的数组,所以不需要映射或任何东西

【问题讨论】:

    标签: java android arrays preferences listpreference


    【解决方案1】:

    我很确定您可以在扩展 PreferenceActivity 的类的 onCreate 方法中使用以下方法:setEntryValuessetEntries。只需向他们传递一个字符串数组即可。

    将 JSONArray 解析为纯字符串数组应该相当容易:

    String[] entries = new String[jsonArray.length()];
    for (int i = 0; i < jsonArray.length(); i++) {
        entries[i] = jsonArray.getString(i);
    }
    

    这可能会失败,具体取决于 JSON 的格式,但这应该会给您大致的思路。

    【讨论】:

      猜你喜欢
      • 2015-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-17
      • 2019-05-14
      • 1970-01-01
      • 2011-09-02
      • 2019-10-04
      相关资源
      最近更新 更多