我有树json对象(家谱)。
我已经引用此链接进行解析: Android parse json tree
但是我从arraylist中获取数据时遇到了问题。
我的所有代码都在以下链接中:
https://app.box.com/s/9ca7bhp04y078wduumoj7j0wkaa4rw40
我想在树结构中显示json数据,并且我正在使用https://github.com/bmelnychuk/AndroidTreeView库。
请指导我解决我的问题。
答案 0 :(得分:0)
1)。将GSON
库导入您的gradle项目:
compile 'com.google.code.gson:gson:2.3.1'
2)。使用http://www.jsonschema2pojo.org/生成你的m,odels
的 Child.java
强>:
public class Child {
@Expose
private String id;
@Expose
private String firstname;
@Expose
private String lastname;
@Expose
private Integer sex;
@Expose
private String image;
@Expose
private List<Child_> children = new ArrayList<Child_>();
/**
*
* @return
* The id
*/
public String getId() {
return id;
}
/**
*
* @param id
* The id
*/
public void setId(String id) {
this.id = id;
}
/**
*
* @return
* The firstname
*/
public String getFirstname() {
return firstname;
}
/**
*
* @param firstname
* The firstname
*/
public void setFirstname(String firstname) {
this.firstname = firstname;
}
/**
*
* @return
* The lastname
*/
public String getLastname() {
return lastname;
}
/**
*
* @param lastname
* The lastname
*/
public void setLastname(String lastname) {
this.lastname = lastname;
}
/**
*
* @return
* The sex
*/
public Integer getSex() {
return sex;
}
/**
*
* @param sex
* The sex
*/
public void setSex(Integer sex) {
this.sex = sex;
}
/**
*
* @return
* The image
*/
public String getImage() {
return image;
}
/**
*
* @param image
* The image
*/
public void setImage(String image) {
this.image = image;
}
/**
*
* @return
* The children
*/
public List<Child_> getChildren() {
return children;
}
/**
*
* @param children
* The children
*/
public void setChildren(List<Child_> children) {
this.children = children;
}
}
<强> Child_.java
强>:
public class Child_ {
@Expose
private String id;
@Expose
private String firstname;
@Expose
private String lastname;
@Expose
private Integer sex;
@Expose
private String image;
/**
*
* @return
* The id
*/
public String getId() {
return id;
}
/**
*
* @param id
* The id
*/
public void setId(String id) {
this.id = id;
}
/**
*
* @return
* The firstname
*/
public String getFirstname() {
return firstname;
}
/**
*
* @param firstname
* The firstname
*/
public void setFirstname(String firstname) {
this.firstname = firstname;
}
/**
*
* @return
* The lastname
*/
public String getLastname() {
return lastname;
}
/**
*
* @param lastname
* The lastname
*/
public void setLastname(String lastname) {
this.lastname = lastname;
}
/**
*
* @return
* The sex
*/
public Integer getSex() {
return sex;
}
/**
*
* @param sex
* The sex
*/
public void setSex(Integer sex) {
this.sex = sex;
}
/**
*
* @return
* The image
*/
public String getImage() {
return image;
}
/**
*
* @param image
* The image
*/
public void setImage(String image) {
this.image = image;
}
}
<强> Datum.java
强>:
public class Datum{
@Expose
private String id;
@Expose
private String firstname;
@Expose
private String lastname;
@Expose
private Integer sex;
@Expose
private String image;
@Expose
private List<Child> children = new ArrayList<Child>();
/**
*
* @return
* The id
*/
public String getId() {
return id;
}
/**
*
* @param id
* The id
*/
public void setId(String id) {
this.id = id;
}
/**
*
* @return
* The firstname
*/
public String getFirstname() {
return firstname;
}
/**
*
* @param firstname
* The firstname
*/
public void setFirstname(String firstname) {
this.firstname = firstname;
}
/**
*
* @return
* The lastname
*/
public String getLastname() {
return lastname;
}
/**
*
* @param lastname
* The lastname
*/
public void setLastname(String lastname) {
this.lastname = lastname;
}
/**
*
* @return
* The sex
*/
public Integer getSex() {
return sex;
}
/**
*
* @param sex
* The sex
*/
public void setSex(Integer sex) {
this.sex = sex;
}
/**
*
* @return
* The image
*/
public String getImage() {
return image;
}
/**
*
* @param image
* The image
*/
public void setImage(String image) {
this.image = image;
}
/**
*
* @return
* The children
*/
public List<Child> getChildren() {
return children;
}
/**
*
* @param children
* The children
*/
public void setChildren(List<Child> children) {
this.children = children;
}
}
<强> Respone.java
强>:
public class Respone{
@Expose
private List<Datum> data = new ArrayList<Datum>();
/**
*
* @return
* The data
*/
public List<Datum> getData() {
return data;
}
/**
*
* @param data
* The data
*/
public void setData(List<Datum> data) {
this.data = data;
}
}
3)。从您的文件:
String strResponse = "...";
在此行之后执行下一步:
Gson gson = new Gson();
Respone response = gson.fromJson(strResponse, Respone.class);
4)。检查您的response
变量。
<强>已更新强>
5)。这是你的json结构:
{
"data":[
{...},
{
...,
"children":[
...,
"children",
...
]
},
{...}
]
}
6)。创建SelectableItem.java
:
public class SelectableItem extends TreeNode.BaseNodeViewHolder<String>{
private final String mText;
public SelectableCategoryItem(Context context, String text) {
super(context);
mText = text;
}
@Override
public View createNodeView(TreeNode treeNode, KalturaCategory c) {
final LayoutInflater inflater = LayoutInflater.from(context);
final View view = inflater.inflate(R.layout.selectable_item_layout, null, false);
TextView someTextViewOrAnythingElse = ((TextView)view.findViewById(R.id.some_textview_or_anything_else));
someTextViewOrAnythingElse.setText(mText);
return view;
}
}
7)。建立你的三个:
public TreeNode buildThree(){
TreeNode node = new TreeNode(response);
SelectableItem item = new SelectableItem(YourActivity.this, "Response");
node.setViewHolder(item);
for(Datum datum : response.getData()){
TreeNode nodeDatum = new TreeNode(datum);
SelectableItem itemDatum = new SelectableItem(YourActivity.this, "Datum: " + datum.getFirstName() + " " + datum.getLastName());
nodeDatum.setViewHolder(itemDatum);
for(Child child : datum.getChildren()){
TreeNode nodeChild = new TreeNode(child);
SelectableItem itemChild = new SelectableItem(YourActivity.this, "Child: " + child.getFirstName() + " " + child.getLastName());
nodeChild.setViewHolder(itemChild);
for(Child_ child_ : child.getChildren()){
TreeNode nodeChild_ = new TreeNode(child_);
SelectableItem itemChild_ = new SelectableItem(YourActivity.this, "Child: " + child_.getFirstName() + " " + child_.getLastName());
nodeChild_.setViewHolder(itemChild_);
}
}
}
TreeNode root = TreeNode.root();
root.addChildren(node);
return root;
}
8)。创建您的ThreeView
:
AndroidTreeView treeView = new AndroidTreeView(YourActivity.this, root);
treeView.setDefaultAnimation(true);
treeView.setDefaultContainerStyle(R.style.TreeNodeStyleCustom);
9)。将您的Tree
添加到版面:
RelativeLayout threeContainer = findViewById(R.id.some_container);
threeContainer.addView(treeView.getView());