I have the following code:
private static ArrayAdapter<String> adapter;
private static List<Chapter> chapters;
public void update(Book book) {
adapter.clear();
if (chapters != null) {
chapters.clear();
}
chapters = DataBaseConnector.getChaptersFromBook(book.getID());
for (Chapter chapter : chapters) {
adapter.add(chapter.getTitle());
}
header.setText(book.getAbbreviation());
subHeader.setText(book.getName() + " (" + book.getNumber() + ")");
subHeader.setVisibility(View.VISIBLE);
}
If I call the method update(Book book) and the variable chapters is not null, I get hit by an UnsupportedOperationException in the line chapters.clear(). Any hints how to solve the problem?
I would have to look at the API documentation, but presumably the
DataBaseConnector.getChaptersFromBook(book.getID())call is returning an Immutable list, so you can’t modify it. It seems you will have to make a local copy.