Removing items from a ListAdapter not working
@Override
public boolean onContextItemSelected(MenuItem item) {
if(item.getTitle()=="Remove"){
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
quotesAdapter.remove(quotes.get((int)info.id));
quotesAdapter.notifyDataSetChanged();
listView.setAdapter(quotesAdapter);
serializeQuotes();
}
else {
return false;
}
return true;
}
Doesn't do开发者_如何学编程 anything. If I add
this.quotesAdapter = new QuoteAdapter(this, R.layout.mainrow, quotes);
Removal works, but I don't think is the right way of doing things. I'm not sure what is wrong?
If quotes
is a Java array, that is unmodifiable at runtime. Try using an ArrayList<>
instead.
If quotes
is a Cursor
, you need to delete the row from the underlying database, then requery()
the Cursor
.
精彩评论