将 JSON 数组(来自 API)添加到 android SDK 上的列表首选项
我需要将从 json api(也可以使用 xml)获得的列表放入列表首选项
谁能尽可能详细地解释一下我该怎么做?
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 & 的正确格式。将此函数添加到数组的方法。
请尽快帮助我
提前致谢
p.s.: 两个条目 &条目值应该是我在这里得到的数组,所以不需要映射或任何东西
I need to put the list I get from a json api (also possible with xml) into a listpreference
could anyone please explain me as good as possible how to do it?
example output from api:
JSON: (preferred)
[
"+32486000001",
"+32486000002" ]
xml: <response>
<resource>+32486000001</resource>
<resource>+32486000002</resource>
</response>
the code I already have to get it:
String username = prefs.getString("username", null);
String password = prefs.getString("password", null);
String response = MVDataHelper.getResponse(username, password, URL_GET_MSISDN);
so the thing I need is to transform the JSonarray to a proper format for the listpreference & a way to add this function to the array.
please help me ASAP
Thanks in advance
p.s.: both entries & entry values should be the array I get here, so no need for mapping or anything
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我很确定您可以在扩展
PreferenceActivity
的类的 onCreate 方法中使用以下方法: setEntryValues 和 设置条目。只需向他们传递一个字符串数组即可。将 JSONArray 解析为普通的字符串数组应该相当容易:
这可能会失败,具体取决于 JSON 的格式,但这应该会给您一个总体思路。
I'm pretty sure you can use the following methods in the onCreate method of a class that extends
PreferenceActivity
: setEntryValues and setEntries. Just pass them an array of strings.Parsing the JSONArray to a plain array of Strings should be fairly easy:
This might fail, depending on the format of your JSON, but that should give you the general idea.