0

如何从以下示例 JSON 输出创建 ListItems?

[
  {
   "section": {
    "term": "Description#1",
    "tid": "10"
  }
  },
  {},
  {},
  {},
  {},
  {},
  {},
  {},
  {
   "section": {
    "term": "Description#2",
    "tid": "12"
  }
 }
]

这个想法是使用每个部分的术语数据作为 listItem 的标签。但是,当单击列表项时,它是为获取与传递的 tid 匹配的数据库结果而检测到的 tid。

4

3 回答 3

1

启动器

public class Launcher {

public static void main(String[] args) {
    String str = "[{" + 
            "   \"section\": {" + 
            "       \"term\": \"Description#1\"," + 
            "       \"tid\": \"10\"" + 
            "   }" + 
            "}," + 
            "{" + 
            "   " + 
            "}," + 
            "{" + 
            "   " + 
            "}," + 
            "{" + 
            "   " + 
            "}," + 
            "{" + 
            "   " + 
            "}," + 
            "{" + 
            "   " + 
            "}," + 
            "{" + 
            "   " + 
            "}," + 
            "{" + 
            "   " + 
            "}," + 
            "{" + 
            "   \"section\": {" + 
            "       \"term\": \"Description#2\"," + 
            "       \"tid\": \"12\"" + 
            "   }" + 
            "}]";

    Gson gson = new Gson();
    Type type = new TypeToken<List<MyObject>>(){}.getType();
    List<MyObject > objList = gson.fromJson(str, type);


    assert(objList != null);

    for(MyObject obj : objList){
      //..
    }
}

 }

我的对象

public class MyObject {
public Section section;   

   public Section getSection() {
    return section;
}  
}

部分

public class Section {
private String term;
private String tid;

public String getTerm() {
    return term;
}
public String getTid() {
    return tid;
}
}

在循环中实现 geters

于 2013-09-30T14:55:08.353 回答
0

通过制作包含该值的哈希映射的数组列表并将该哈希映射添加到您的数组列表中来尝试此操作

于 2013-09-30T15:00:31.513 回答
0

您是否可以不只是将 JSON 数据转换为 JSONObject 获取 TID 并将它们放入数组列表中,在列表适配器中设置术语并设置点击监听器以从数组列表中的位置返回 tid?

于 2013-09-30T15:25:07.413 回答