开发者

Removing items from a ListAdapter not working

开发者 https://www.devze.com 2023-01-13 18:21 出处:网络
@Override public boolean onContextItemSelected(MenuItem item) { if(item.getTitle()==\"Remove\"){ AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
@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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号