views:

17

answers:

1

I have a ListView that is filled from my adapter with my custom views. Each view has two buttons, one that starts another activity to edit the contents of that list item and one to delete that item.

My question is where should my ClickEvent handlers for those buttons be? Should I put them right in my custom view code since I have all the information I need? Should I start an ASyncTask that deletes the item and updates the adapter data and calls onDataSetChanged() etc?

Should all of this be bubbled up through events to my ListActivity?

I could probably make it work at any level, but at what level along this hierarchy (Activity->ListView->Adapter->ListItemView) is the proper place to edit/delete backing data for the ListView?

A: 

I do it this way:

  • Adapters have public methods for underlying data manipulation (e.g. public void deleteItem(int position)) that do their job and call notifyDataSetChanged() in the end;
  • Activity keeps a reference to it's Adapter in a local variable;
  • Listeners are usually created as anonymous inner classes of Activity calling appropriate Adapter methods.
alex