Reputation: 1794
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?
Upvotes: 1
Views: 89
Reputation: 11923
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"));
Upvotes: 1