-2

I have tree json object(Family tree).

I have refer this link for parsing : Android parse json tree

But i got problem while fetching data from arraylist.

My all code in below link :

https://app.box.com/s/9ca7bhp04y078wduumoj7j0wkaa4rw40

I want to display json data in tree structure and for that i am using https://github.com/bmelnychuk/AndroidTreeView library.

Please guide me to solve my problem.

1 Answer 1

0

1). Import GSON library to your gradle project:

compile 'com.google.code.gson:gson:2.3.1'

2). Generate your m,odels using http://www.jsonschema2pojo.org/
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). From your file:

String strResponse = "...";

After this line do next:

Gson gson = new Gson();
Respone response = gson.fromJson(strResponse, Respone.class);

4). Check your response variable.

UPDATED

5). Here is your json structure:

{
    "data":[
        {...},
        {
            ...,
            "children":[
                ...,
                "children",
                ...
            ]
        },
        {...}
    ]
}

6). Create 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). Build your three:

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). Create your ThreeView:

AndroidTreeView treeView = new AndroidTreeView(YourActivity.this, root);
treeView.setDefaultAnimation(true);
treeView.setDefaultContainerStyle(R.style.TreeNodeStyleCustom);

9). Add your Tree to layout:

RelativeLayout threeContainer = findViewById(R.id.some_container);
threeContainer.addView(treeView.getView());
Sign up to request clarification or add additional context in comments.

1 Comment

Alex : Here which TreeNode class is used?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.