【问题标题】:How do I map this list of objects to an expandable listview?如何将此对象列表映射到可扩展的列表视图?
【发布时间】:2016-12-12 02:49:12
【问题描述】:

我正在使用 jackson 从 json 创建对象,我想在 expandableListView.

json 看起来像这样:

[
  {
    "name": "Pete",
    "profile_img": "img001",
    "relations": {
      "is_working_with": [],
      "is_in_course_with": [
        {
          "profile_img": "img002",
          "name": "Jack"
        }
      ],
      "is_friends_with": [
        {
          "profile_img": "img003",
          "name": "Stacey"
        },
        {
          "profile_img": "img002",
          "name": "Jack"
        }
      ]
    }
  },
  {
    "name": "Jack",
    "profile_img": "img002",
    "relations": {
      "is_working_with": [{
        "profile_img": "img003",
        "name": "Stacey"
      }],
      "is_in_course_with": [
        {
          "profile_img": "img001",
          "name": "Pete"
        }
      ],
      "is_friends_with": [
        {
          "profile_img": "img003",
          "name": "Stacey"
        },
        {
          "profile_img": "img001",
          "name": "Pete"
        }
      ]
    }
  }
]

我的 java 类是:

public class Student {
    @JsonProperty("name")
    String name;
    @JsonProperty("profile_img")
    String profileImg;
    @JsonProperty("relations")
    Relations relations;

    //Constructors
    //Setter, Getter
}
public class Relations {
    @JsonProperty("is_working_with")
    List<IsWorkingWith> isWorkingWith;
    @JsonProperty("is_friends_with")
    List<IsFriendsWith> isFriendsWith;
    @JsonProperty("is_in_course_with")
    List<IsInCourseWith> isInCourseWith;

    //Constructors
    //Setter, Getter
}
public class IsWorkingWith {
    @JsonProperty("name")
    String name;
    @JsonProperty("profile_img")
    String profileImg;
}
public class IsFriendsWith {
    @JsonProperty("name")
    String name;
    @JsonProperty("profile_img")
    String profileImg;
}
public class IsInCourseWith {
    @JsonProperty("name")
    String name;
    @JsonProperty("profile_img")
    String profileImg;
}

我试图扩展 BaseExpandableListAdapter 并且我让它适用于组视图,但是我如何指定我的 Student 对象的哪个属性代表列表视图中的子项。

基本上我希望它看起来像这样:

我应该如何在子视图中生成项目?

【问题讨论】:

    标签: java android json listview expandablelistview


    【解决方案1】:

    鉴于您为ExpandableListView 说明的方式,孩子们是关系类的成员。您有一个额外的维度,即关系类中的每个成员都有自己的列表。

    首先,让我们修正你的类定义:

    public class Relations {
        @JsonProperty("is_working_with")
        List<Person> isWorkingWith;
        @JsonProperty("is_friends_with")
        List<Person> isFriendsWith;
        @JsonProperty("is_in_course_with")
        List<Person> isInCourseWith;
    }
    
    public class Person {
        @JsonProperty("name")
        String name;
        @JsonProperty("profile_img")
        String profileImg;
    }
    

    如果每个关系都有自己的属性,即,在课程中的人有他们共享课程的属性,希望您的 JSON 注释可以处理单独的 Person 子类。

    举个例子,让我们以getChildrenCount()

        @Override
        public int getChildrenCount(int groupPosition) {
            return 3; // because every student has three types of relations
        }
    

    现在你来看看更复杂的部分。你有一个自己有一个列表的孩子。所以子视图必须处理不同数量的人员。对此有不同的选择,但如果我必须选择,我会让子视图成为水平 RecyclerView。

        @Override
        public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View view, ViewGroup parent) {
    
            // first let's handle the model stuff
            Student student = (Student) getGroup(groupPosition);  // would need to be implemented
            List<Person> persons;
            switch (childPosition) {
            case 0:
                persons = student.isWorkingWith;
                break;
            case 1:
                persons = student.isFriendsWith;
                break;
            case 2:
                persons = student.isInCourseWith;
                break;
            }
    
            // now set up the view/adapter
            RecyclerView recyclerView;
            PersonAdapter adapter;   // you would need to write this
            if (view == null) {
                view = LayoutInflater.from(mContext).inflate(R.layout.child, parent, false);
                recyclerView = (RecyclerView) view.findViewById(R.id.recyclerview);
                view.setTag("recyclerview", recyclerView);
                adapter = new PersonAdapter(parent.getContext()); // no data yet
                view.setTag("adapter", adapter);
                recyclerView.setLayoutManager(new LinearLayoutManager(parent.getContext(), LinearLayoutManager.HORIZONTAL, false);
                recyclerView.setAdapter(adapter);
            } else {
                recyclerView = (RecyclerView) view.getTag("recyclerview");
                adapter = (PersonAdapter) view.getTag("adapter");
            }
    
            adapter.setData(persons); // this method needs to call notifyDataSetChanged()
    
            return view;
        }
    

    所以你最终会得到嵌套的基于适配器的视图。

    代替RecyclerView,您可以动态添加/删除您的Person 视图(不推荐),或者您可以扩展一个有十几个Person 视图的水平LinearLayout,然后隐藏您没有结束的视图使用。但是RecyclerView 方法更简单一些。

    无论如何,希望这足以让您入门。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-01-24
      • 2019-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-16
      • 2012-03-28
      相关资源
      最近更新 更多