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());
开发者_如何学Python 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.
According to the Android docs (and JavaDocs as well for the List interface):
"Throws UnsupportedOperationException if removing from this List is not supported."
My guess is the implementation that is being used does not support the clear() method.
精彩评论