Editable ListView in Android - as is in iOS
how would one create something like editable UITableView on iPhone?
I need to be able to switch the list to edit mode, then select multiple items and then do an action - delete, move to another list.
If there is no built-in or open source alternative, I would probably need to code it myself.
I think that I could add a (at first invisible) checkbox to the XML layout of each ListView item, and after the user clicks "Edit", I would set a flag in the list adapter to make the checkboxes visible and show the Move and Delete buttons.
When the user then clicks the Move/Delete button, I would somehow collect indexes of the checked items (see this answer) and do the actions.
If the user exits the edit mode, I would make the checkboxes gone again.
My question is, how would I modify all items in the listview to show checkboxes? I can set a flag in my adapter, and in getView I would set visib开发者_如何学编程ility of that checkbox. But how would I force the listview to get all views (call getView for each) - invalidate it?
You can use contextual menu like in these tutorials:
http://www.miximum.fr/tutos/849-porting-the-contextual-anction-mode-for-pre-honeycomb-android-apps
http://androidfornewbies.blogspot.com/2012/05/14-tutorial-using-contextual-action.html
http://wptrafficanalyzer.in/blog/creating-a-contextual-menu-bar-contextual-action-mode-for-a-single-view-in-android/
Edit:
you can delete multiple as in the first link and here is a quick code that should delete all selected items.
List<String> titles = new ArrayList<String>();
...
SparseBooleanArray checked = mListView.getCheckedItemPositions();
for (int i = 0 ; i < checked.size(); i++) {
if(checked.valueAt(i))
//delete that item from the source array of the listView
// then notify the adapter with change.
titles.remove(i);
}
you may have to look at this first, if haven't EFFECIENT ADAPTER ... in its onListItemClick()
you can do your stuff based on the view selected...
精彩评论