Как получить номер записи из массива объекта, который содержит собственный массив - PullRequest
0 голосов
/ 15 июня 2019

Я хочу n число, которое означает мою последнюю запись ArrayList, но проблема в том, что мой ArrayList имеет собственный класс POJO, который содержит свой собственный массив, поэтому я не могу определить, сколько записей в ArrayList содержит

Пожалуйста, проверьте ниже мой JSON, из которого я создаю класс POJO

{
  "data": [
    {
      "id": "7",
      "name": "Grand Father Name",
      "parent_id": "0",
      "relation_type": "grand-father",
      "children": [

      ]
    },
    {
      "id": "8",
      "name": "Grand Mother Name",
      "parent_id": "0",
      "relation_type": "grand-mother",
      "children": [
        {
          "id": "9",
          "name": "Father Name",
          "parent_id": "8",
          "relation_type": "father",
          "children": [
            {
              "id": "11",
              "name": "My Name",
              "parent_id": "9",
              "relation_type": "self",
              "children": [

              ]
            }
          ]
        },
        {
          "id": "10",
          "name": "Mother Name",
          "parent_id": "8",
          "relation_type": "mother",
          "children": [

          ]
        }
      ]
    }
  ]
}

класс POJO

public class Tree {
    @SerializedName("data")
    @Expose
    public ArrayList<Child> data = null;

    public class Child {
        @SerializedName("family_id")
        @Expose
        public int family_id;
        @SerializedName("name")
        @Expose
        public String name;
        @SerializedName("relation_type")
        @Expose
        public String relation_type;
        @SerializedName("parent_id")
        @Expose
        public int parentId;
        @SerializedName("children")
        @Expose
        public List<Child> children = null;

        public int getId() {
            return family_id;
        }

        public void setId(int id) {
            this.family_id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getParentId() {
            return parentId;
        }

        public void setParentId(int parentId) {
            this.parentId = parentId;
        }

        public List<Child> getChildren() {
            return children;
        }

        public void setChildren(List<Child> children) {
            this.children = children;
        }
    }
}

Как вы можете видеть, дочерний класс содержит собственный список, поэтому я не могуопределите, сколько записей будет в этом списке

Надеюсь, это решит мою проблему, любая помощь будет оценена.Заранее спасибо

1 Ответ

1 голос
/ 15 июня 2019

Вы можете сделать это рекурсивно:

ArrayList<Child> list = new ArrayList<>(); //this is the list where you want to fill all children

void fillRecursively(ArrayList<Child> root){
 List<Child> children = root.getChildren();
 for(Child child : children){
   if(child.getChildren().size()!=0){
    fillRecursively(child);
   }else{
   list.add(child);
   }
 }
}
}

Также не забудьте добавить свой корневой узел в список.

list.add(root); //at the beginning
...