1

I am trying to add another item to an existing List<> in my Android application. My List<> is initialised here:

List<ListsRecyclerViewList> list = new Arrays.asList(newListsRecyclerViewList("item", "item"));

The ListsRecyclerViewList class looks like this:

public String name;
public String date;

public ListsRecyclerViewList(String name, String date) {
    this.name = name;
    this.date = date;
}

public String getName() {
    return name;
}

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

public String getDate() {
    return name;
}

public void setDate(String date) {
    this.date = date;
}

Is there any way to add another item to the List<>? Any suggestions on how to achieve this?

2
  • 1
    You can just wrap it in new ArrayList<ListsRecyclerViewList>(Arrays.asList(...)) also... Commented Feb 2, 2016 at 20:22
  • 1
    did you, a/ try? b/ read the doc? Commented Feb 2, 2016 at 20:28

1 Answer 1

1

The List returned from using Arrays.asList(...) cannot have its size modified as explained in its documentation:

Returns a List of the objects in the specified array. The size of the List cannot be modified, i.e. adding and removing are unsupported, but the elements can be set. Setting an element modifies the underlying array.

Do this instead:

List<ListsRecyclerViewList> list = new ArrayList<>();
list.add(newListsRecyclerViewList("item", "item"));

and then sometime later:

list.add(newListsRecyclerViewList("anotherItem", "anotherItem"));
Sign up to request clarification or add additional context in comments.

Comments

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.