0

I am trying to add elements to a list

here is my parent class

public class TodoParent {
    public String ParentTitle;
    public Integer ParentId;
    public List<ToDoModel> mChildList;
    public long EpochTime;

    public TodoParent(String mTitle,List<ToDoModel> childModels) {

        this.ParentTitle = mTitle;
        this.mChildList = childModels;
    }

    public TodoParent() {

    }

    public String getParentTitle() {
        return ParentTitle;
    }
    public Integer getParentId() {
        return ParentId;
    }
    public List<ToDoModel> getmChildList() {
        return mChildList;
    }
    public void setParentTitle(String title) {
        ParentTitle = title;
    }
    public void setParentId(Integer mId) {
        ParentId = mId;
    }
    public void setmChildList(List<ToDoModel> list) {
        mChildList = list;
    }
}

Here is the situation where i need to add element

@Override
public void applyTexts(String header, String footer, String listType, String timeOfDay, int priotity,long mEpochtime) {

    //Toast.makeText(this,(String) timeOfDay, Toast.LENGTH_SHORT).show();

    ToDoModel toDoModel = new ToDoModel();
    TodoParent todoParent = new TodoParent();

    toDoModel.header = header;
    toDoModel.footer = footer;
    toDoModel.task = listType;
    toDoModel.tod = timeOfDay;
    toDoModel.priorityVal = priotity;
    toDoModel.epochTime = mEpochtime;
    toDoModel.id = toDoModelList.size() + 1;
    todoParent.ParentTitle = footer;
    todoParent.mChildList.add(toDoModel);
    todoParent.EpochTime = mEpochtime;


    Toast.makeText(getActivity(), "New List Item Added", Toast.LENGTH_SHORT).show();

    //mAdapter.add(toDoModelList.size(), toDoModel);
    parentAdapter.add(todoParentList.size(),todoParent);
    //input.add("To Do New1");
}

todoParent.mChildList.add(toDoModel) is not working. How can I add toDoModel to mChildList ?

I am new to java and this is something i am working on as a training. So any help is appreciated.Thankyou

chand mohd
  • 2,363
  • 1
  • 14
  • 27
Cod3r
  • 17
  • 5

2 Answers2

0

Yes, because you are invoking empty constructor of ToDoParent.

public TodoParent() {

}

You are not initializing there List.You have to add new ArrayList<>() to this constructor and it will work.

Zeeshan Ali
  • 667
  • 8
  • 16
Freeride
  • 1
  • 1
0

Try something like this:

 public class TodoParent {
        public String ParentTitle;
        public ArrayList<ToDoModel> mChildList;
        public long EpochTime;

        public TodoParent() {
            this.ParentTitle = "";
            this.mChildList = new ArrayList<>();
        }

        public TodoParent(String mTitle, ArrayList<ToDoModel> childModels) {

            this.ParentTitle = mTitle;
            this.mChildList = childModels;
        }
    }

    public class ToDoModel {

        public String header;

        public ToDoModel() {
            this.header = "";
        }

        public ToDoModel(String header) {
            this.header = header;
        }
    }

Use ArrayList instead of List. Now if you print something like that

 System.out.println("ToDoParent mChildList size = " + todoParent.mChildList.size());

you can see that the size of mChildList is 1 as you add one item.

ziselos
  • 494
  • 5
  • 15