0

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 visibility of that checkbox. But how would I force the listview to get all views (call getView for each) - invalidate it?

enter image description here

Community
  • 1
  • 1
Axarydax
  • 16,353
  • 21
  • 92
  • 151

2 Answers2

1

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);
    }
Khaled Annajar
  • 15,542
  • 5
  • 34
  • 45
0

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...

ngesh
  • 13,398
  • 4
  • 44
  • 60