我用杰克逊和json创建对象,我想在一个中显示这些对象
$('#comparison').click(function (event) {
event.preventDefault();
$('#year, #country, #dish').prop('disabled', true);
});
。
json看起来像这样:
expandableListView
我的java课程是:
[
{
"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"
}
]
}
}
]
我试图扩展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
对象的哪个属性代表列表视图中的子项。
基本上我希望它看起来像这样:
我应该如何在子视图中生成项目?
答案 0 :(得分:0)
鉴于您对ExpandableListView
进行说明的方式,孩子们是“关系”类的成员。您有一个额外的维度,即Relations类中的每个成员都有自己的列表。
首先,让我们修复您的类定义:
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;
}
如果每个关系都有自己的属性,即in-course-with具有他们共享的课程的属性,希望您的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
,您可以动态添加/删除您的个人观点(不推荐),或者您可以对具有十几个人观看次数的水平LinearLayout
进行充气,然后隐藏您所观看的观看次数#39; t最终使用。但RecyclerView
方法稍微简单一点。
无论如何,希望这足以让你开始。